我有一个问题,最好是说如何思考这个问题的最佳解决办法。 我的CSV文件看起来像:
,02/12/2013,03/12/2013,04/12/2013,05/12/2013,06/12/2013,07/12/2013,08/12/2013,
06:00,"06:00 World Sport","06:00 World Sport","06:00 World Sport","06:00 World Sport","06:00 World Sport","06:00 World Sport","06:00 World Sport",06:00
,,,,,,,,
06:15,,,,,,,,06:15
,,,,,,,,
06:30,"06:30 Inside Africa: November 29, 2013","06:30 African Voices: Agatha Achindu","06:30 Inside the Louvre","06:30 Talk Asia: Franz Harary","06:30 Blueprint","06:30 Inside the Middle East","06:30 CNNGo",06:30
好的,我需要做的是,在一张纸上编辑从1到多少的日期, 把日期写在每一行的开始前面,在逗号之前,就像这个例子:
^{pr2}$我的起始代码是这样的:
尝试:
for line in fileinput.input(fnames):
if re.search(r'\d{2}/\d{2}/\d{4}.*',line):
line_date = re.findall(r'\d{2}/\d{2}/\d{4}',line)[0]
output.write(line_date+'\n')
if re.search(r'\".+?\"',line):
line_sadrzaj = re.findall(r'\".+?\"',line)[0]
output.write(line_sadrzaj+'\n')
output.close()
你对这个问题有什么更好的想法吗。在
也许这样:
for line in fileinput.input(fnames):
if re.search(r'\d{2}/\d{2}/\d{4}.*',line):
line_date = re.findall(r'\d{2}/\d{2}/\d{4}.*',line)[0]
line_split = re.split(r'\,',line_date)
for line1 in line_split:
var = line1
output.write(var+'\n')
if re.search(r'\".+?\".*',line):
line_sadrzaj = re.findall(r'\".+?\".*',line)[0]
line_split1 = re.split (r'\,',line_sadrzaj)
for line2 in line_split1:
var2 = line2
output.write(var2+'\n')
#output.write(line_sadrzaj+'\n'
您根本不需要regex;只需使用
csv
模块读取csv文件,然后将结果转换为所需的输出。在示例:
结果:
^{pr2}$相关问题 更多 >
编程相关推荐