Python将TXT转换为CSV

时间:2016-01-11 21:46:58

标签: python csv

我一直在尝试将txt文件转换为CSV,但一直遇到麻烦。

我的文本文件采用以下格式:

POP  Issue: key=u'VPER-242', id=u'167782' 
POP  Issue: key=u'TE-8', id=u'215771' 
POP  Issue: key=u'OUTDIAL-233', id=u'223166' 
POP  Issue: key=u'OUTDIAL-232', id=u'223047'

目标是将其放入CSV文件中,该文件如下所示,包含2列:

Name of issue                         
POP  Issue: key=u'VPER-242'    
POP  Issue: key=u'TE-8'
POP  Issue: key=u'OUTDIAL-233'
POP  Issue: key=u'OUTDIAL-232'

Issue ID
id=u'167782'
id=u'215771'
id=u'223166'
id=u'223047'

基本上使用" ,"在txt文件中充当分隔符并将两者分成列。以下代码用于将列名称放在我的CSV顶部以及拆分,但它的格式不正确,并且不会被"分开。 ,"。

import csv
import itertools
with open('newfile1.txt', 'r') as in_file:
    stripped = (line.strip() for line in in_file)
    lines = (line for line in stripped if line)
    grouped = itertools.izip(*[lines] * 2)
    with open('newfile1.csv', 'w') as out_file:
            writer = csv.writer(out_file)
            writer.writerow(('Name of Issue', 'Issue ID'))
            writer.writerows(grouped)

这是这段代码输出的内容 - 虽然很接近但不太正确。我不想要空格,并且问题ID列只需要ID = u'数字'数据和问题名称仅包含POP问题数据。有人有什么建议吗?谢谢!

Name of Issue   
POP  Issue: key=u'VPER-242', id=u'167782'

POP  Issue: key=u'TE-8', id=u'215771'

POP  Issue: key=u'OUTDIAL-233', id=u'223166'


Issue ID
POP  Issue: key=u'TE-8', id=u'215771'

POP  Issue: key=u'OUTDIAL-232', id=u'223047'

POP  Issue: key=u'OUTDIAL-229', id=u'222309'

3 个答案:

答案 0 :(得分:0)

您的代码只是使用itertools.izip将同一个数组压缩在一起,这就是为什么它在两列下打印相同的结果。你需要拆分逗号,然后继续前进。

答案 1 :(得分:0)

import csv

txt_file = r"YourTextDocument.txt"
csv_file = r"NewProcessedDoc.csv"

in_txt = csv.reader(open(txt_file, "rb"), delimiter = ',')
out_csv = csv.writer(open(csv_file, 'wb'))

out_csv.writerows(in_txt)
print 'done! go check your NewProcessedDoc.csv file'

# You can insert new rows manually in your csv for the titles (Name of issue & Issue ID) 

答案 2 :(得分:-2)

已编辑:更多详情

简短回答:

替换它:

grouped = itertools.izip(*[lines] * 2)

用这个:

grouped = [line.split(',') for line in lines]

更长的答案:

你的"分组"变量包含重复行对(不是你想要的)

如果您的输入行不包含任何其他逗号(","),那么str.split是您此任务的朋友。

干杯