Python 文本文件之间的日期/时间转换
我有一个水文模型文本文件输出 (export.txt),如下所示:
Units CFS
类型 INST-VAL
1997年1月1日, 02:00 1933.0
2 1997年1月1日, 04:00 1918.0
3 1997年1月1日 06:00 1918.0
4 1997年1月1日, 08:00 1904.0
5 1997年1月1日 10:00 1904.0
...
并让 Python (2.6) 编码以下内容以格式化输入优化过程:
import re
o=open("C:\documents and settings\cmjawdy\desktop\PyOut.txt","w")
data=open("C:\documents and settings\cmjawdy\desktop\export.txt").read()
Step1=re.sub(":00",":00:00",data)
Step2=re.sub(" Jan ","/01/",Step1)
Step3=re.sub(",","",Step2)
FindIDs=re.compile("^[0-9]*\s",re.M)
Step4=re.sub(FindIDs,"SiteXXX ",Step3)
o.write(Step4)
o.close()
产量:
单位 CFS
类型 INST-VAL
SiteXXX 01/01/1997 02:00:00 1933.0
SiteXXX 01/01/1997 04:00:00 1918.0
SiteXXX 01/01/1997 06:00:00 1918.0
SiteXXX 01/01/1997 08:00:00 1904.0
SiteXXX 01/01/1997 10:00:00 1904.0
...
问题是我的优化软件不能以24为小时,而必须以00为第二天的小时。因此,我需要将 X 天的 24:00:00 转换为 X+1 天的 00:00:00,同时保持相同的格式。看起来 strptime/strftime 也不需要 24。这些是我对任何计算机语言的绝对第一行,我找不到转换此文本的优雅方法。
I have a hydrologic model text file output (export.txt) that looks like:
Units CFS
Type INST-VAL
1 01 Jan 1997, 02:00 1933.0
2 01 Jan 1997, 04:00 1918.0
3 01 Jan 1997, 06:00 1918.0
4 01 Jan 1997, 08:00 1904.0
5 01 Jan 1997, 10:00 1904.0
...
And have Python (2.6) coded the following to format for input to an optimization process:
import re
o=open("C:\documents and settings\cmjawdy\desktop\PyOut.txt","w")
data=open("C:\documents and settings\cmjawdy\desktop\export.txt").read()
Step1=re.sub(":00",":00:00",data)
Step2=re.sub(" Jan ","/01/",Step1)
Step3=re.sub(",","",Step2)
FindIDs=re.compile("^[0-9]*\s",re.M)
Step4=re.sub(FindIDs,"SiteXXX ",Step3)
o.write(Step4)
o.close()
Yielding:
Units CFS
Type INST-VAL
SiteXXX 01/01/1997 02:00:00 1933.0
SiteXXX 01/01/1997 04:00:00 1918.0
SiteXXX 01/01/1997 06:00:00 1918.0
SiteXXX 01/01/1997 08:00:00 1904.0
SiteXXX 01/01/1997 10:00:00 1904.0
...
The problem is that my optimization software can't take 24 as the hour, rather it must take 00 as the hour on the following day. So I need to convert 24:00:00 on day X to 00:00:00 on day X+1 while maintaining the same format. It looks as though strptime/strftime don't take 24 either. These are my absolute first lines of any computer language and I can't find an elegant way to convert this text.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
结果:
result:
以下代码解决了某些特定日期后的下一天的问题,以查找一行中何时有“24:00”:1 月 31 日、2 月 28 日、2 月 29 日、6 月 30 日、12 月 31 日等
结果
The following code solves the problem of the day next after some particular days to find when there is '24:00' in a line: January 31, February 28, February 29, June 30, December 31, etc
result