Python 文本文件之间的日期/时间转换

发布于 2024-12-08 08:58:50 字数 1068 浏览 1 评论 0原文

我有一个水文模型文本文件输出 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

年华零落成诗 2024-12-15 08:58:50
import datetime
s = '''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
6 01 Jan 1997, 24:00 1000.0'''
for row in s.split('\n'):
    prefix = row[:2]
    sdate = row[2:-7]
    suffix = row[-7:]
    if sdate[13:15] == '24':
        offset = datetime.timedelta(1)
        sdate = sdate[:13] + '00' + sdate[15:]
    else:
        offset = datetime.timedelta(0)
    dt = datetime.datetime.strptime(sdate, '%d %b %Y, %H:%M') + offset
    print prefix + dt.strftime('%d/%m/%Y %H:%M:%S') + suffix

结果:

1 01/01/1997 02:00:00 1933.0
2 01/01/1997 04:00:00 1918.0
3 01/01/1997 06:00:00 1918.0
4 01/01/1997 08:00:00 1904.0
5 01/01/1997 10:00:00 1904.0
6 02/01/1997 00:00:00 1000.0
import datetime
s = '''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
6 01 Jan 1997, 24:00 1000.0'''
for row in s.split('\n'):
    prefix = row[:2]
    sdate = row[2:-7]
    suffix = row[-7:]
    if sdate[13:15] == '24':
        offset = datetime.timedelta(1)
        sdate = sdate[:13] + '00' + sdate[15:]
    else:
        offset = datetime.timedelta(0)
    dt = datetime.datetime.strptime(sdate, '%d %b %Y, %H:%M') + offset
    print prefix + dt.strftime('%d/%m/%Y %H:%M:%S') + suffix

result:

1 01/01/1997 02:00:00 1933.0
2 01/01/1997 04:00:00 1918.0
3 01/01/1997 06:00:00 1918.0
4 01/01/1997 08:00:00 1904.0
5 01/01/1997 10:00:00 1904.0
6 02/01/1997 00:00:00 1000.0
只是在用心讲痛 2024-12-15 08:58:50

以下代码解决了某些特定日期后的下一天的问题,以查找一行中何时有“24:00”:1 月 31 日、2 月 28 日、2 月 29 日、6 月 30 日、12 月 31 日等

import re


ss1 = '''Units CFS 
Type INST-VAL
1 01 Jan 1997, 02:00 1933.0
2 12 Feb 1997, 04:00 1918.0
3 26 May 1997, 06:00 1918.0
4 15 Aug 1997, 08:00 1904.0
5 09 Dec 1997, 10:00 1904.0'''

ss2 = '''Units CFS 
Type INST-VAL
1 31 Jan 1997, 11:00 1933.0
2 28 Feb 1997, 11:00 1918.0
2 29 Feb 1997, 11:00 1918.0
3 31 Mar 1997, 11:00 1918.0
4 30 Sep 1997, 11:00 1904.0
5 31 Dec 1997, 11:00 1904.0'''

ss3 = '''Units CFS 
Type INST-VAL
1 31 Jan 1997, 24:00 1933.0
2 28 Feb 2011, 24:00 1700.2
2 29 Feb 2011, 24:00 1700.0
2 28 Feb 2012, 24:00 1801.8
2 29 Feb 2012, 24:00 1801.0
3 31 Mar 1997, 24:00 1918.0
4 30 Sep 1997, 24:00 1904.0
5 31 Dec 1997, 24:00 1904.0'''


bis = ('1904', '1908', '1912', '1916', '1920', '1924', '1928', '1932', '1936', '1940',
       '1944', '1948', '1952', '1956', '1960', '1964', '1968', '1972', '1976', '1980',
       '1984', '1988', '1992', '1996', '2000', '2004', '2008', '2012', '2016', '2020',
       '2024', '2028', '2032', '2036', '2040', '2044', '2048', '2052', '2056', '2060',
       '2064', '2068', '2072', '2076', '2080', '2084', '2088', '2092', '2096', '2104')

months = dict(zip('Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'.split(),xrange(1,13)))

firstday_nextmonth = {('31','Jan'):'01/02/',                        ('31','Mar'):'01/04/',
                      ('30','Apr'):'01/05/', ('31','May'):'01/06/', ('30','Jun'):'01/07/',
                      ('31','Jul'):'01/08/', ('31','Aug'):'01/09/', ('30','Sep'):'01/10/',
                      ('31','Oct'):'01/11/', ('30','Nov'):'01/12/', ('31','Dec'):'01/01/'}


di = {'1':'MADRID ','2':'HUAHINE','3':'MOSCOW ','4':'OSAKA  ','5':'VALPAR.'}


def repl(mat, fdnm = firstday_nextmonth, months = months, sites = di, bisextiles = bis):
    d,m,y = mat.group(2,3,4)

    if mat.group(5)=='24:00':
        if (d,m)==('31','Dec'):
            dmy = '01/01/%d' % (int(y)+1)
        elif (d,m) == ('29','Feb'):
            if y in bisextiles:
                dmy = '01/03/' + y
            else:
                dmy = '!!!!!!!!!!'
        elif (d,m)==('28','Feb'):
            if y in bisextiles:
                dmy = '29/02/' + y
            else:
                dmy = '01/03/' + y
        elif (d,m) in fdnm:
            dmy = fdnm[(d,m)] + y
        else:
            dmy = '%02d/%02d/%s' % (int(mat.group(2))+1,months[m],y)

    elif (d,m) == ('29','Feb') and y not in bisextiles:
        dmy = '!!!!!!!!!!'

    else:
        dmy = '%s/%02d/%s' % (d,months[m],y)

    return '%s %s %s:00' % (sites[mat.group(1)],
                            dmy,
                            mat.group(5).replace('24:00','00:00'))  




reg = re.compile('^(\d+) ([012]\d|30|31) ([a-z]+) (\d{4}), (\d\d:\d\d)(?= \d+.\d+)',
                 re.IGNORECASE|re.MULTILINE)

for ss in (ss1,ss2,ss3):
    print ss
    print
    print reg.sub(repl,ss)
    print '\n=========================================================\n'

结果

Units CFS 
Type INST-VAL
1 01 Jan 1997, 02:00 1933.0
2 12 Feb 1997, 04:00 1918.0
3 26 May 1997, 06:00 1918.0
4 15 Aug 1997, 08:00 1904.0
5 09 Dec 1997, 10:00 1904.0

Units CFS 
Type INST-VAL
MADRID  01/01/1997 02:00:00 1933.0
HUAHINE 12/02/1997 04:00:00 1918.0
MOSCOW  26/05/1997 06:00:00 1918.0
OSAKA   15/08/1997 08:00:00 1904.0
VALPAR. 09/12/1997 10:00:00 1904.0

=========================================================

Units CFS 
Type INST-VAL
1 31 Jan 1997, 11:00 1933.0
2 28 Feb 1997, 11:00 1918.0
2 29 Feb 1997, 11:00 1918.0
3 31 Mar 1997, 11:00 1918.0
4 30 Sep 1997, 11:00 1904.0
5 31 Dec 1997, 11:00 1904.0

Units CFS 
Type INST-VAL
MADRID  31/01/1997 11:00:00 1933.0
HUAHINE 28/02/1997 11:00:00 1918.0
HUAHINE !!!!!!!!!! 11:00:00 1918.0
MOSCOW  31/03/1997 11:00:00 1918.0
OSAKA   30/09/1997 11:00:00 1904.0
VALPAR. 31/12/1997 11:00:00 1904.0

=========================================================

Units CFS 
Type INST-VAL
1 31 Jan 1997, 24:00 1933.0
2 28 Feb 2011, 24:00 1700.2
2 29 Feb 2011, 24:00 1700.0
2 28 Feb 2012, 24:00 1801.8
2 29 Feb 2012, 24:00 1801.0
3 31 Mar 1997, 24:00 1918.0
4 30 Sep 1997, 24:00 1904.0
5 31 Dec 1997, 24:00 1904.0

Units CFS 
Type INST-VAL
MADRID  01/02/1997 00:00:00 1933.0
HUAHINE 01/03/2011 00:00:00 1700.2
HUAHINE !!!!!!!!!! 00:00:00 1700.0
HUAHINE 29/02/2012 00:00:00 1801.8
HUAHINE 01/03/2012 00:00:00 1801.0
MOSCOW  01/04/1997 00:00:00 1918.0
OSAKA   01/10/1997 00:00:00 1904.0
VALPAR. 01/01/1998 00:00:00 1904.0

=========================================================

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

import re


ss1 = '''Units CFS 
Type INST-VAL
1 01 Jan 1997, 02:00 1933.0
2 12 Feb 1997, 04:00 1918.0
3 26 May 1997, 06:00 1918.0
4 15 Aug 1997, 08:00 1904.0
5 09 Dec 1997, 10:00 1904.0'''

ss2 = '''Units CFS 
Type INST-VAL
1 31 Jan 1997, 11:00 1933.0
2 28 Feb 1997, 11:00 1918.0
2 29 Feb 1997, 11:00 1918.0
3 31 Mar 1997, 11:00 1918.0
4 30 Sep 1997, 11:00 1904.0
5 31 Dec 1997, 11:00 1904.0'''

ss3 = '''Units CFS 
Type INST-VAL
1 31 Jan 1997, 24:00 1933.0
2 28 Feb 2011, 24:00 1700.2
2 29 Feb 2011, 24:00 1700.0
2 28 Feb 2012, 24:00 1801.8
2 29 Feb 2012, 24:00 1801.0
3 31 Mar 1997, 24:00 1918.0
4 30 Sep 1997, 24:00 1904.0
5 31 Dec 1997, 24:00 1904.0'''


bis = ('1904', '1908', '1912', '1916', '1920', '1924', '1928', '1932', '1936', '1940',
       '1944', '1948', '1952', '1956', '1960', '1964', '1968', '1972', '1976', '1980',
       '1984', '1988', '1992', '1996', '2000', '2004', '2008', '2012', '2016', '2020',
       '2024', '2028', '2032', '2036', '2040', '2044', '2048', '2052', '2056', '2060',
       '2064', '2068', '2072', '2076', '2080', '2084', '2088', '2092', '2096', '2104')

months = dict(zip('Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'.split(),xrange(1,13)))

firstday_nextmonth = {('31','Jan'):'01/02/',                        ('31','Mar'):'01/04/',
                      ('30','Apr'):'01/05/', ('31','May'):'01/06/', ('30','Jun'):'01/07/',
                      ('31','Jul'):'01/08/', ('31','Aug'):'01/09/', ('30','Sep'):'01/10/',
                      ('31','Oct'):'01/11/', ('30','Nov'):'01/12/', ('31','Dec'):'01/01/'}


di = {'1':'MADRID ','2':'HUAHINE','3':'MOSCOW ','4':'OSAKA  ','5':'VALPAR.'}


def repl(mat, fdnm = firstday_nextmonth, months = months, sites = di, bisextiles = bis):
    d,m,y = mat.group(2,3,4)

    if mat.group(5)=='24:00':
        if (d,m)==('31','Dec'):
            dmy = '01/01/%d' % (int(y)+1)
        elif (d,m) == ('29','Feb'):
            if y in bisextiles:
                dmy = '01/03/' + y
            else:
                dmy = '!!!!!!!!!!'
        elif (d,m)==('28','Feb'):
            if y in bisextiles:
                dmy = '29/02/' + y
            else:
                dmy = '01/03/' + y
        elif (d,m) in fdnm:
            dmy = fdnm[(d,m)] + y
        else:
            dmy = '%02d/%02d/%s' % (int(mat.group(2))+1,months[m],y)

    elif (d,m) == ('29','Feb') and y not in bisextiles:
        dmy = '!!!!!!!!!!'

    else:
        dmy = '%s/%02d/%s' % (d,months[m],y)

    return '%s %s %s:00' % (sites[mat.group(1)],
                            dmy,
                            mat.group(5).replace('24:00','00:00'))  




reg = re.compile('^(\d+) ([012]\d|30|31) ([a-z]+) (\d{4}), (\d\d:\d\d)(?= \d+.\d+)',
                 re.IGNORECASE|re.MULTILINE)

for ss in (ss1,ss2,ss3):
    print ss
    print
    print reg.sub(repl,ss)
    print '\n=========================================================\n'

result

Units CFS 
Type INST-VAL
1 01 Jan 1997, 02:00 1933.0
2 12 Feb 1997, 04:00 1918.0
3 26 May 1997, 06:00 1918.0
4 15 Aug 1997, 08:00 1904.0
5 09 Dec 1997, 10:00 1904.0

Units CFS 
Type INST-VAL
MADRID  01/01/1997 02:00:00 1933.0
HUAHINE 12/02/1997 04:00:00 1918.0
MOSCOW  26/05/1997 06:00:00 1918.0
OSAKA   15/08/1997 08:00:00 1904.0
VALPAR. 09/12/1997 10:00:00 1904.0

=========================================================

Units CFS 
Type INST-VAL
1 31 Jan 1997, 11:00 1933.0
2 28 Feb 1997, 11:00 1918.0
2 29 Feb 1997, 11:00 1918.0
3 31 Mar 1997, 11:00 1918.0
4 30 Sep 1997, 11:00 1904.0
5 31 Dec 1997, 11:00 1904.0

Units CFS 
Type INST-VAL
MADRID  31/01/1997 11:00:00 1933.0
HUAHINE 28/02/1997 11:00:00 1918.0
HUAHINE !!!!!!!!!! 11:00:00 1918.0
MOSCOW  31/03/1997 11:00:00 1918.0
OSAKA   30/09/1997 11:00:00 1904.0
VALPAR. 31/12/1997 11:00:00 1904.0

=========================================================

Units CFS 
Type INST-VAL
1 31 Jan 1997, 24:00 1933.0
2 28 Feb 2011, 24:00 1700.2
2 29 Feb 2011, 24:00 1700.0
2 28 Feb 2012, 24:00 1801.8
2 29 Feb 2012, 24:00 1801.0
3 31 Mar 1997, 24:00 1918.0
4 30 Sep 1997, 24:00 1904.0
5 31 Dec 1997, 24:00 1904.0

Units CFS 
Type INST-VAL
MADRID  01/02/1997 00:00:00 1933.0
HUAHINE 01/03/2011 00:00:00 1700.2
HUAHINE !!!!!!!!!! 00:00:00 1700.0
HUAHINE 29/02/2012 00:00:00 1801.8
HUAHINE 01/03/2012 00:00:00 1801.0
MOSCOW  01/04/1997 00:00:00 1918.0
OSAKA   01/10/1997 00:00:00 1904.0
VALPAR. 01/01/1998 00:00:00 1904.0

=========================================================
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文