【问题标题】:Converting filename from julian to gregorian date, and reinserting back into filename将文件名从儒略日期转换为公历日期,然后重新插入文件名
【发布时间】:2015-08-14 19:46:12
【问题描述】:

我有一堆这样的文件名:

LT50300281984137PAC00_sr_band3.tif

LT50300281985137PAC00_sr_band1.tif

我想将每个文件名的 [9:16] 中包含的儒略日期更改为公历日期,然后将新日期重新插入文件名。我已使用此代码将朱利安转换为公历:

import datetime, glob, os
    for raster in glob.glob('r'F:\Sheyenne\Atmospherically Corrected Landsat\hank_masked\julian_dates/*.tif'):
        year=int(oldFilename[9:13])
    #the day
        day=int(oldFilename[13:16])
    #convert to julian date
        date=datetime.datetime(year,1,1)+datetime.timedelta(day)
        print date

这会给我每个文件的儒略日期,所以对于像这样的文件LT50300281984137PAC00_sr_band3.tif,这将返回1984-05-17 00:00:00,但我不想要00:00:00,我想插入公历日期回到文件名,最好是19840517

编辑:

使用到目前为止所有答案的建议,我可以做任何事情,但使用以下命令执行重命名(本示例中的最后一行代码):

import datetime, glob, os
        for raster in glob.glob(r'F:\Sheyenne\Atmospherically Corrected Landsat\hank_masked\julian_dates/*.tif'):
    oldFilename=raster
    year=int(oldFilename[9:13])
    #the day
    day=int(oldFilename[13:16])
    #convert to julian date
    date=datetime.datetime(year,1,1)+datetime.timedelta(day)
    #generate newfile names
    newFilename=oldFilename[:9] + date.strftime('%Y%m%d') + oldFilename[16:]
    #rename the files
    os.rename(oldFilename, newFilename) 

这会返回错误:WindowsError: [Error 2] The system cannot find the file specified,我认为这可能与我的 os 路径有关。到目前为止,所有其他变量都按预期填充。

编辑:此代码适用于我

arcpy.env.workspace=r'F:\Sheyenne\Atmospherically Corrected Landsat\hank_masked\julian_dates'
hank_bands='F:\Sheyenne\Atmospherically Corrected Landsat\hank_masked\julian_dates'
hank_out='F:\Sheyenne\Atmospherically Corrected Landsat\hank_masked\greg_dates'
list1=arcpy.ListRasters("*.tif")
for raster in list1:
    source_path = os.path.join(hank_bands, raster)
    oldFilename=raster
    year=int(oldFilename[9:13])
    #the day
    day=int(oldFilename[13:16])
    #convert to julian date
    date=datetime.datetime(year,1,1)+datetime.timedelta(day)
    newFilename=oldFilename[:9] + date.strftime('%Y%m%d') + oldFilename[16:]
    destination_path=os.path.join(hank_out, newFilename)
    os.rename(source_path, destination_path) 

【问题讨论】:

  • (1) 尝试将您的问题限制在一个问题上,即 WindowsError 应该作为一个单独的问题提出。否则,它会使问题对未来的访问者不太有用。 (2)不要在问题中回答,post your own answer (and accept it if you'd like) instead
  • 旁白:以 YYYYddd 形式表示的时间,其中 YYYY 是年份,ddd 是年份中的天数,而不是儒略日期。儒略日期是自公元前 4713 年 1 月 1 日世界标准时间中午以来的天数(预测儒略历)。 python datetime 模块不处理儒略日期。其他 python 模块,例如 astropy 也可以。
  • @DavidHammen:"julian" means different things in different cases。在这种情况下,"julian" 是来自 time 模块的“从零开始的儒略日”。

标签: python datetime


【解决方案1】:

您可以为此使用正则表达式:

import re
import os

filename = 'LT50300281984137PAC00_sr_band3.tif'
oldDate = re.sub('(LT5030028)|(PAC00_sr_band3.tif)','',filename) # Extracts the old date
# calculate new date from old date
# newDate = '1984-05-17 00:00:00'
newDate = re.sub('(-)|( .*)','',newDate) # Removes the dashes and the time
newFilename = filename.replace(oldDate,newDate) # Replaces the old date by the new date

os.rename(filename, newFilename) # renames the file to the new file name

【讨论】:

  • 如果我想将LT5030028PAC00_sr_band3.tif 设置为变量,这将如何更改oldDate = re.sub('(LT5030028)|(PAC00_sr_band3.tif)','',filename) 行?
  • 应该是oldDate = re.sub('('+varA+')|('+varB+')','',filename)
【解决方案2】:

一旦你有yearday 方法strftime 给出你的结果。对于 1984 和 137,您会得到:

import datetime

date=datetime.date(year,1,1)+datetime.timedelta(day)
printf(date.strftime("%4Y%2m%2d"))

19840517

所以你现在可以这样做了:

newFilename = oldFilename[:9] + date.strftime("%4Y%2m%2d") + oldFilename[16:]

【讨论】:

    【解决方案3】:

    要将字符串转换为日期对象,请使用datetime.strptime()

    >>> from datetime import datetime
    >>> datetime.strptime('1985137', '%Y%j')
    datetime.datetime(1985, 5, 17, 0, 0)
    >>> datetime.strptime('1984137', '%Y%j')
    datetime.datetime(1984, 5, 16, 0, 0)
    

    注意:输入的解释不同:1984137 在这里是 1984-05-16137 被解释为一年中的一天,其中 1 月 1 日是第 1 天),而您问题中的 datetime(year, 1, 1) + timedelta(day) 公式暗示 @ 987654329@ 是从零开始的(两种情况都计算为 2 月 29 日)。

    要将日期对象转换为字符串,请使用.strftime() 方法:

    >>> datetime(1985, 5, 17, 0, 0).strftime('%Y%m%d')
    '19850517'
    

    替换文件名中的固定位置:

    >>> filename = 'LT50300281984137PAC00_sr_band3.tif'
    >>> filename[9:16]
    '1984137'
    >>> new_name = filename[:9] + '19840516' + filename[16:]
    >>> new_name
    'LT503002819840516PAC00_sr_band3.tif'
    

    如果目标可能位于不同的文件系统上,要重命名文件,请使用shutil.move()

    >>> import shutil
    >>> shutil.move(os.path.join(src_dir, filename), os.path.join(dest_dir, new_name))
    

    如果目标文件可能已经存在;拨打os.remove(dest_path)

    【讨论】:

      猜你喜欢
      • 2013-07-21
      • 1970-01-01
      • 2012-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-02
      • 1970-01-01
      相关资源
      最近更新 更多