Python:使用儒略日期重命名文件的嵌套循环
Python: Nested loops for renaming files with julian dates
我有大约 30,000 个从 1971 年到 2070 年的每日温度栅格 (.asc),需要重命名。它们目前的命名如下:tasmax_0.asc、tasmax_1.asc、....、tasmax_32767.asc.
我需要用儒略日期和年份重命名它们(即 tasmax_1_1971.asc、tasmax_2_1971.asc、....、tasmax_365_2070.asc)。
我知道我需要使用带有不同计数器的嵌套循环:一个儒略日计数器(需要在每年年初重置)和一个年计数器。我很容易与嵌套循环混淆,尤其是在闰年有 366 天而不是 365 天的情况下,我必须每年重置儒略日计数器。
我正在使用 python 2.7
如果能帮助我全神贯注地编写此脚本,我们将不胜感激!
提前致谢!
也许您正在寻找这样的东西:
import os
filex = 0
year = 1971
while filex < 32768:
if (( year%400 == 0)or (( year%4 == 0 ) and ( year%100 != 0))):
days = 366
else:
days = 365
current_day = 1
while current_day <= days:
os.rename(("tasmax_" + str(filex) + ".asc"),(("tasmax_" + str(current_day) + str(year) + ".asc")))
current_day = current_day + 1
filex = filex + 1
year = year + 1
文件编号、一年中的天数、当前日期和当前年份的计数器。
使用
重命名文件 im
os.rename(oldfilename, newfilename)
但使用您喜欢的任何东西。
此示例仅打印出 1_1971
、2_1971
等
from datetime import date, timedelta
day = date(1971, 1, 1) #1 Jan 1971
step = timedelta(1) #1 day
curr_year = day.year
count = 1
while day.year <= 2070:
print("{}_{}".format(count, curr_year))
day += step
count += 1
if day.year != curr_year:
count = 1
curr_year = day.year
您可以使用 Python 的 datetime
模块。
import os
import datetime
start_date = datetime.datetime(1971, 1, 1) # January 1, 1971
for fname in os.listdir("date_folder"): # Iterate through filenames
num_days = fname.split("_")[1] # Check number of days from start
cur_date = start_date + datetime.timedelta(days=num_days) # Add number of days to start
os.rename(fname, "tasmax_{0}_{1}".format(cur_date.day, cur_date.year)) # Reformat filename
这假设所有文件都在一个目录中。
我有大约 30,000 个从 1971 年到 2070 年的每日温度栅格 (.asc),需要重命名。它们目前的命名如下:tasmax_0.asc、tasmax_1.asc、....、tasmax_32767.asc.
我需要用儒略日期和年份重命名它们(即 tasmax_1_1971.asc、tasmax_2_1971.asc、....、tasmax_365_2070.asc)。
我知道我需要使用带有不同计数器的嵌套循环:一个儒略日计数器(需要在每年年初重置)和一个年计数器。我很容易与嵌套循环混淆,尤其是在闰年有 366 天而不是 365 天的情况下,我必须每年重置儒略日计数器。
我正在使用 python 2.7
如果能帮助我全神贯注地编写此脚本,我们将不胜感激!
提前致谢!
也许您正在寻找这样的东西:
import os
filex = 0
year = 1971
while filex < 32768:
if (( year%400 == 0)or (( year%4 == 0 ) and ( year%100 != 0))):
days = 366
else:
days = 365
current_day = 1
while current_day <= days:
os.rename(("tasmax_" + str(filex) + ".asc"),(("tasmax_" + str(current_day) + str(year) + ".asc")))
current_day = current_day + 1
filex = filex + 1
year = year + 1
文件编号、一年中的天数、当前日期和当前年份的计数器。
使用
重命名文件 imos.rename(oldfilename, newfilename)
但使用您喜欢的任何东西。
此示例仅打印出 1_1971
、2_1971
等
from datetime import date, timedelta
day = date(1971, 1, 1) #1 Jan 1971
step = timedelta(1) #1 day
curr_year = day.year
count = 1
while day.year <= 2070:
print("{}_{}".format(count, curr_year))
day += step
count += 1
if day.year != curr_year:
count = 1
curr_year = day.year
您可以使用 Python 的 datetime
模块。
import os
import datetime
start_date = datetime.datetime(1971, 1, 1) # January 1, 1971
for fname in os.listdir("date_folder"): # Iterate through filenames
num_days = fname.split("_")[1] # Check number of days from start
cur_date = start_date + datetime.timedelta(days=num_days) # Add number of days to start
os.rename(fname, "tasmax_{0}_{1}".format(cur_date.day, cur_date.year)) # Reformat filename
这假设所有文件都在一个目录中。