简体   繁体   English

将YYYYMMDD文件名转换为YYYYJD

[英]Convert YYYYMMDD filename to YYYYJD

I'm trying to write a python script to convert a folder of .asc files (365 files for every year in different folders organized by year) that have the yearmonthdate in their filename to have the yearjuliandate instead and the julian date needs to be 3 digits (ie 1 = 001). 我正在尝试编写一个Python脚本来转换.asc文件的文件夹(每年365个文件,按年组织的不同文件夹中),其文件名中具有yearmonthdate,以具有yearjuliandate,而朱利安日期必须为3数字(即1 = 001)。 The format they are in: ETos19810101.asc. 它们采用的格式:ETos19810101.asc。 I want them to be as: ETos1981001.asc 我希望他们是:ETos1981001.asc

How do I write this in Python where I can iterate over each file and convert it to the correct julian day? 如何在Python中编写此代码,在其中可以遍历每个文件并将其转换为正确的儒略日?

I'm trying to write a Python script to convert a folder of .asc files (365 files for every year in different folders organized by year) that have the yearmonthdate in their filename to have the yearjuliandate instead and the julian date needs to be 3 digits (ie 1 = 001). 我正在尝试编写一个Python脚本来转换.asc文件的文件夹(每年365个文件,按年组织的不同文件夹),其文件名中具有yearmonthdate,以具有yearjuliandate,而julian date需要为3数字(即1 = 001)。

  • The format they are in: ETos19810101.asc 它们所采用的格式: ETos19810101.asc
  • I want them to be as: ETos1981001.asc 我希望他们是: ETos1981001.asc

How do I write this in Python where I can iterate over each file and convert it to the correct julian day? 如何在Python中编写此代码,在其中可以遍历每个文件并将其转换为正确的儒略日?

I have this so far: 到目前为止,我有:

import os.path, os, glob

for filename in glob.glob(filepath + "/*.asc"):
    jdate = '%03d' %doy #creates 3 digit julian date
    doy = doy + 1
    filename.replace(int[-8:-4], jdate + 1)

Given a file name as following (you can iterate your file system with os.walk ) 给定以下文件名(您可以使用os.walk迭代文件系统)

filename = 'ETos19810101.asc'

First of all you have to split the filename to get every significant parts: 首先,您必须拆分文件名以获取所有重要部分:

import os
name, ext = os.path.splitext(filename)
prefix = name[0:-6] # negative prefix => string end as reference
strdate = name[-6:]

Then you can parse the date: 然后,您可以解析日期:

from datetime import datetime
date = datetime.strptime(strdate, '%Y%m%d')

Now you are able to join everything together ( %Y%j format the date the way you want): 现在,您可以将所有内容连接在一起( %Y%j以所需的方式格式化日期):

newfilename = '{prefix}{date:%Y%j}{ext}'.format(prefix=prefix, date=date, ext=ext)

Finally rename the file: 最后重命名文件:

os.rename(filename, newfilename)

Note that the last instruction will fail if newfilename already exists. 请注意,如果newfilename已经存在,则最后一条指令将失败。 To fix this issue you have to remove the file, if it exists: 要解决此问题,您必须删除该文件(如果存在):

if os.path.exists(newfilename):
    os.remove(newfilename)
os.rename(filename, newfilename)

For working with dates you should use the datetime module . 要处理日期,应使用datetime模块 Parse the date string with strptime . strptime解析日期字符串。 There's no function to return a julian date, but it's easy to create one: 没有返回Julian日期的功能,但是创建日期很容易:

def julian_day(dt):
    jan1 = dt.replace(month=1, day=1)
    return 1 + (dt - jan1).days

Use the '%j specifier along with datetime.strptime and os.rename and the various os.path commands: '%j specifierdatetime.strptimeos.rename以及各种os.path命令一起使用:

from datetime import datetime
from glob import glob
import os

for filename in glob(os.path.join(filepath, 'ETos*.asc')):
    try:
        dt = datetime.strptime(os.path.basename(filename), 'ETos%Y%m%d.asc')
    except ValueError as e:
        continue # rest of file name didn't have valid date - do the needful
    os.rename(filename, os.path.join(filepath, format(dt, 'ETos%Y%j.asc')))

You'll probably want a bit of handling around that and adjust to take into account your path, but that's the general principle. 您可能需要对此进行一些处理,并进行调整以考虑您的路径,但这是一般原则。

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

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