提问人:Md Aatif Hasan 提问时间:10/20/2023 最后编辑:BarmarMd Aatif Hasan 更新时间:10/20/2023 访问量:32
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
问:
#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"""
答:
2赞
Barmar
10/20/2023
#1
问题是逗号后面的空格。CSV 中的字段允许使用引号,但由于该字段以空格开头,因此引号似乎不会围绕整个字段值。因此,它们被视为需要在输出中加倍的文字引号。
使用该选项忽略这些空格。skipinitialspace
csv_reader=csv.reader(csv_file, skipinitialspace=True)
评论