[英]format the different types of date in CSV using python 2.6
I have formatted my csv file and now it looks like this: 我已经格式化了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|
Now I have two different format of dates which I want to convert it into 'YYmmdd' format. 现在,我有两种不同的日期格式,我想将其转换为“ YYmmdd”格式。
Can any one suggest best way to achieve this. 谁能建议实现此目标的最佳方法。 Note: The file name should not get change and for your reference this is how I am achieveing the formatted file which is given here: 注意:文件名不应更改,这是我如何获得此处给出的格式化文件供您参考:
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)
I think this should work. 我认为这应该有效。 You can tweak the date format anyway you like by changing the strftime. 您可以通过更改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
if you have Python 2.6+ you could do it just in python 如果您拥有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|
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.