python中的csv编写器在从另一个csv文件复制数据时添加双引号,所以我只需要知道如何复制完全相同的数据

csv writer in python adding double quote when copying data from another csv file, So I just need to know how to copy exactly the same data

提问人:Md Aatif Hasan 提问时间:10/20/2023 最后编辑:BarmarMd Aatif Hasan 更新时间:10/20/2023 访问量:32

问:

#Reading from csv file
with open('py_csv/oscar_age_male.csv','r') as csv_file:
    csv_reader=csv.reader(csv_file)

    #Creating new file and opening it and changing delimiter
    with open('py_csv/newfile.csv','w')as newfile:
        csv_writer=csv.writer(newfile)

#Writing Row by row
        for line in csv_reader:
            csv_writer.writerow(line)

原始文件中的数据=

1, 1928, 44, "Emil Janning", "The Last Command, The Way of All Flesh"
2, 1929, 41, "Warner Baxter", "In Old Arizona"

复制后的数据=

1, 1928, 44," ""Emil Janning"""," ""The Last Command"," The Way of All Flesh"""
2, 1929, 41," ""Warner Baxter"""," ""In Old Arizona"""
python csv 解析 引用

评论

1赞 Ulrich Eckhardt 10/20/2023
就像逗号和换行符一样,引号是格式的一部分。第一个文件的实际逐字内容是什么?关于引用的阅读和写作活动设置是什么?顺便说一句:可以用不同的方式表示相同的数据,比如 0.5=1/2。

答:

2赞 Barmar 10/20/2023 #1

问题是逗号后面的空格。CSV 中的字段允许使用引号,但由于该字段以空格开头,因此引号似乎不会围绕整个字段值。因此,它们被视为需要在输出中加倍的文字引号。

使用该选项忽略这些空格。skipinitialspace

csv_reader=csv.reader(csv_file, skipinitialspace=True)