例如,我有 28 个文件:-
文件->day01.nc
内部文件:-
| lat | lon | date |
| --- | --- | -------- |
| 7 | 68 | 2021-02-01|
| 7 | 69 | 2021-02-01|
文件->day02.nc
内部文件:-
| lat | lon | date |
| --- | --- | -------- |
| 7 | 68 | 2021-02-02|
| 7 | 69 | 2021-02-02|
文件->day28.nc
内部文件:-
| lat | lon | date |
| --- | --- | -------- |
| 7 | 68 | 2021-02-28|
| 7 | 69 | 2021-02-28|
我想使用 python 脚本根据日期格式转换他们的名字,比如temp_ind20210201.nc
, temp_ind20210202.nc
,...。temp_ind20210328.nc
注意: -该day01.nc
文件内的日期格式是2021-02-01
这样的。我正在尝试:-
DATA_DIR = 'data'
today = datetime.datetime.now()
offset_1day = datetime.timedelta(days=1)
re_number = re.compile('day(\d{,2})\.nc')
for fname in glob.glob(DATA_DIR + "/*.nc"):
number_string = re_number.search(fname)
if not number_string:
continue
number_of_days = int(number_string.group(1))
str_timestamp = (today + (number_of_days - 1) * offset_1day).strftime("%Y%m%d")
new_fname = f"{DATA_DIR}/temp_ind{_str_timestamp}.nc"
print(f'{fname} -> {new_fname}')
os.rename(fname, new_fname)