gpt4 book ai didi

python - CSV 编写器双引号

转载 作者:太空宇宙 更新时间:2023-11-04 09:27:37 24 4
gpt4 key购买 nike

我在 Python 中有一个字符串:你好,我叫“Joe”。当我尝试使用 csv 模块进行编写时,我得到了 “你好,我的名字是”“Joe”””。我希望看到的是“你好,我叫“Joe””

当存在双引号时,是否可以让 CSV 编写器不添加双引号?

代码:

s = 'hello, my name is "Joe"'
with open(filename, 'w', newline='', encoding='utf-8') as f_out:
writer = csv.writer(f_out)
writer.writerow([s])

最佳答案

您可以在创建 csv.writer ( doc ) 时使用 quotechar 参数:

import csv

s = 'hello, my name is "Joe"'
with open('out.csv', 'w', newline='', encoding='utf-8') as f_out:
writer = csv.writer(f_out, quotechar="'")
writer.writerow([s])

文件的输出是:

'hello, my name is "Joe"'

关于python - CSV 编写器双引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56959550/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com