【发布时间】:2022-01-26 10:41:01
【问题描述】:
我正在尝试使用 csv 模块将每个字段值括在双引号中。 但这里的诀窍是我们在需要跳过的值之间有逗号。 这是我用来将值括在引号中的 sn-p。
数据:
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
代码sn-p
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 文件中有多少列?
-
@Tbaki 这就是我们的客户 API 的创建方式,他们使用分隔符作为逗号
-
@Corralien 实际上是 2
-
@jennie 如果您的数据集中有“,”和数据中的“,”,使用它作为分隔符不是一个好习惯,请尝试让他们将其更改为不如果您可以联系到他们,请显示在数据集中;