繁体   English   中英

IF 语句应用于特定 position 中的字符串的 substring

[英]IF-statement applied to substring of a string in a specific position

我正在使用一堆*.tif文件,这些文件的名称如下: [20120101.tif, 20120102.tif, 20120103.tif,...] (即YEARMONTHDAY.tif )。 问题是我必须只处理与某些特定月份相对应的文件(例如 JAN、FEB、MAR、NOV、DEC)。 我打算使用IF语句,但我不知道如何应用它才能只工作几个月。 贝娄,我展示了我的代码。

Root_dir = r"f:\myTifFiles"
os.chdir(Root_dir)
List_tif = glob.glob("*.tif")

for i in range(len(List_tif)):
    if ... #in this line I was planning to call a conditional statement to work only with some specific months
        Raster = gdal.Open(List_tif[i])
        geotransform = Raster.GetGeoTransform()

我将不胜感激

List_tif = glob.glob("*.tif")
months_to_process = ['01','02','11']  # months you have to work with.

for image in List_tif:
    # extract a substring at index 4,5 example: 20220123.tif ==> 01
    # then check if the extracted value is in the months you need to process.
    # Then you can do what you need
    if image[4:6] in months_to_process:
        Raster = gdal.Open(List_tif[i])
        geotransform = Raster.GetGeoTransform()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM