簡體   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