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

eni9jsuy  于 7个月前  发布在  Python
关注(0)|答案(1)|浏览(56)
#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"""

zvms9eto

zvms9eto1#

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

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

字符串

相关问题