【问题标题】:format the different types of date in CSV using python 2.6使用 python 2.6 在 CSV 中格式化不同类型的日期
【发布时间】:2015-10-04 11:06:52
【问题描述】:

我已经格式化了我的 csv 文件,现在它看起来像这样:

100|1000|newyork|2015/10/04|2015/10/04 16:23:37.040000|

101|1001|london|2015/10/04|2015/10/04 16:23:37.040000|

102|1002|california|2015/10/04|2015/10/04 16:23:37.041000|

103|1003|Delhi|2015/10/04|2015/10/04 16:23:37.041000|

104|1004|Mumbai|2015/10/04|2015/10/04 16:23:37.041000|

105|1005|Islamabad|2015/10/04|2015/10/04 16:23:37.041000|

106|1006|karachi|2015/10/04|2015/10/04 16:23:37.041000|

现在我有两种不同格式的日期,我想将其转换为 'YYmmdd' 格式。

任何人都可以建议实现这一目标的最佳方法。 注意:文件名不应更改,供您参考,这是我实现此处给出的格式化文件的方式:

inputfile = 'c:\Working\HK.txt'

outputfile = inputfile + '.tmp'
with contextlib.nested(open(inputfile, 'rb'), open(outputfile, 'wb')) as (inf,outf):
    reader = csv.reader(inf)
    writer = csv.writer(outf, delimiter='|')
    for row in reader:
        writer.writerow([col.replace('|', ' ') for col in row])
        writer.writerow([])
os.remove(inputfile)
os.rename(outputfile,inputfile)

【问题讨论】:

  • 那么为什么不使用正确的格式模式以第一种方式对其进行格式化:%y%m%d
  • @Daniel 我从其他具有这种格式的系统中获得了这个日期,而我无法做到这不在我手中。所以寻找我们可以的东西。 :)
  • 如果您从文件中读取并构建 CSV,我会在那里修复它。如果您知道结构,只需在日期列上使用 parse 和 strftime('%Y%m%d')。
  • 是的,我正在从 csv 文件中读取它,然后对其进行格式化,因为我是 python 新手,所以我可能会问一些愚蠢的问题,如果我得到提示它会很好,这样我也可以从我的最后,尝试总比问好。

标签: python csv python-2.6


【解决方案1】:

我认为这应该可行。您可以通过更改 strftime 来调整日期格式。

#!/usr/bin/python
from dateutil.parser import parse

lines = ['100|1000|newyork|2015/10/04|2015/10/04 16:23:37.040000|',
         '101|1001|london|2015/10/04|2015/10/04 16:23:37.040000|',
         '102|1002|california|2015/10/04|2015/10/04 16:23:37.041000|',
         '103|1003|Delhi|2015/10/04|2015/10/04 16:23:37.041000|',
         '104|1004|Mumbai|2015/10/04|2015/10/04 16:23:37.041000|',
         '105|1005|Islamabad|2015/10/04|2015/10/04 16:23:37.041000|',
         '106|1006|karachi|2015/10/04|2015/10/04 16:23:37.041000|']

for line in lines:
    parts = line.split("|");

    tmp_date = parse(parts[3])
    parts[3] = tmp_date.strftime('%Y%m%d') 

    tmp_date = parse(parts[4])
    parts[4] = tmp_date.strftime('%Y%m%d')

    new_line = "|".join(parts) 
    print new_line

【讨论】:

    【解决方案2】:

    如果你有 Python 2.6+,你可以在 python 中完成

    from __future__ import print_function
    import re
    
    with open('data','r') as f, open('data_out', 'w') as f_out:
    
        for line in f:
            line = re.sub('(|\d{4})/(\d{2})/(\d{2})',r'\1\3\2', line)
            line = re.sub('\s+\d{2}:\d{2}:\d{2}.\d+(|)',r'\1', line)
    
            print(line, file=f_out)
    
    this is what i got in my data_out
    
    100|1000|newyork|20151004|20151004|
    101|1001|london|20151004|20151004|
    102|1002|california|20151004|20151004|
    103|1003|Delhi|20151004|20151004|
    104|1004|Mumbai|20151004|20151004|
    105|1005|Islamabad|20151004|20151004|
    106|1006|karachi|20151004|20151004|
    

    【讨论】:

    • @cyborg 尝试将 print(line, file=f_out) 更改为 print >>f_out,line 或 f_out.write(line)
    猜你喜欢
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    • 2020-05-27
    • 2019-08-04
    • 1970-01-01
    • 2016-02-23
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多