本文介绍了编写具有多个变量的CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有两个列表:

lat = [34.78, 34.82, 34.86, 34.92]
lon = [-86.02, -86.06, -86.10, -86.14]

我正在尝试编写一个将其输出为经/纬度的csv文件:

I am trying to write a csv file that outputs them as lat/lon:

34.78, -86.02
34.82, -86.06
34.86, -86.10
34.92, -86.14

我尝试使用:

with open('lat_lon', 'w') as csvfile:
    writer=csv.writer(csvfile, delimiter=',')
    writer.writerow(lat)
    writer.writerow(lon)

但这给了我一行的所有lat值和另一行的lon值.关于如何纠正这一点的任何想法?谢谢!

But this gives me the lat values all in one row and the lon values in a separate row. Any ideas on how to correct this? Thanks!

推荐答案

您只需要 zip 并使用 writerows 而不是writerow:

You just need to zip and use writerows instead of writerow:

with open('lat_lon', 'w') as csvfile:
    writer=csv.writer(csvfile, delimiter=',')
    writer.writerows(zip(lat, lon))

这篇关于编写具有多个变量的CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 15:35