我有这个csv文件:
未格式化地址,格式化地址,纬度,lng"3400 Main Street Prescott,Arizona 86303 United States"3400 Main Street Prescott,AZ 86303,USA";,34.1234567,-112.7654321
以下代码旨在将新行添加到csv文件的末尾:
with open('Geocode_test.csv', 'a+', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=['unformatted_address', 'formatted_address', 'lat', 'lng'])
row = {'unformatted_address': '70 Orchard Ave Cancun New York 10555',
'formatted_address': '70 Orchard Ave Cancun NY 10555 USA',
'lat': 100.1,
'lng': 200.2}
writer.writerow(row)
writer.writerow(row)
csvfile.close()
我预料到了这个结果:
未格式化地址,格式化地址,纬度,lng"3400 Main Street Prescott,Arizona 86303 United States"3400 Main Street Prescott,AZ 86303,USA";,34.1234567、-112.7654321美国纽约州坎昆乌节大道70号,邮编:10555,70,邮编:105.55,邮编:100.1200.270 Orchard Ave Cancun New York 10555,70 Orchard大道Cancun NY 10555 USA,100.1200.2
相反,我得到了这样的结果:
未格式化地址,格式化地址,纬度,lng"3400 Main Street Prescott,Arizona 86303 United States"3400 Main Street Prescott,AZ 86303,USA";,34.1234567,-112.765432170 Orchard Ave Cancun New York 10555,70 Orchard大道Cancun NY 10555 USA,100.1200.270 Orchard Ave Cancun New York 10555,70 Orchard大道Cancun NY 10555 USA,100.1200.2
请注意,第一个writerow()
似乎将第一个字段连接到csv的最后一个条目,然后将后续条目写入右侧的列中。第二个CCD_ 2的行为与预期的一样。
原始csv在文件末尾缺少换行符。