用python中的双引号将每个中间有逗号的CSV字段括起来



我正试图使用csv模块将每个字段值用双引号括起来。但这里的诀窍是,我们确实在需要跳过的值之间有逗号。这是我用来将值括在引号中的代码段。

数据:

col1,col2
first row,This section of the badge focuses on Communications
second row,Feedback has partnered with team members, leaders, and executives receive confidential, anonymous feedback

代码段

import csv
with open('data.csv') as input, open('out.csv','w') as output:
reader = csv.reader(input)
writer = csv.writer(output, delimiter=',', quoting=csv.QUOTE_ALL)
for line in reader:
writer.writerow(line)

输出

"col1","col2"
"first row","This section of the badge focuses on Communications"
"second row","Feedback has partnered with team members"," leaders"," and executives receive confidential"," anonymous feedback"

预期输出

"col1","col2"
"first row","This section of the badge focuses on Communications"
"second row","Feedback has partnered with team members, leaders, and executives receive confidential, anonymous feedback"

由于输入数据不是常规CSV文件,因此使用csv模块读取输入文件可能会出现问题。为了解决这个问题,您可以直接读取文件的行,然后按如下方式解析它们:

import csv
with open('data.csv') as fin, open('out.csv','w') as fout:
writer = csv.writer(fout, delimiter=',', quoting=csv.QUOTE_ALL)
for line in fin:
writer.writerow(line.rstrip().split(',', 1))

您可以将DictReaderDictWriterrestkey属性一起使用:

with open('data.csv') as inp, open('out.csv', 'w') as out:
reader = csv.DictReader(inp, restkey='colN')
writer = csv.DictWriter(out, fieldnames=reader.fieldnames,
delimiter=',', quoting=csv.QUOTE_ALL)
writer.writeheader()
for line in reader:
line[reader.fieldnames[-1]] += ','.join(line.pop('colN', []))
writer.writerow(line)

out.csv:的内容

"col1","col2"
"first row","This section of the badge focuses on Communications"
"second row","Feedback has partnered with team members leaders, and executives receive confidential, anonymous feedback"

最新更新