编写带有多个变量的CSV,带有字面名称Python

发布于 2025-01-22 17:52:10 字数 537 浏览 3 评论 0原文

我是Python的新手,所以请友善...我需要编写一个带有多个变量和字段名称的CSV文件。我找到了不同的帖子: 的CSV

编写具有多个变量 。我知道那一定是这样的:

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

如果您能帮助我,那就太好了!

顺便说一句,我在Mac上使用Python 3.10.4和Pycharm Community Edition 2021.3.3。

谢谢!

Im very new to python so please be kind... I need to write a csv file with multiple variables and a fieldname. I found a different post:
Writing a CSV with multiple variables

But i have 0 clue how to add a fieldname. I know that it has to be something like:

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

if you can help me that would be great!

btw I'm using python 3.10.4 and pycharm community edition 2021.3.3 on mac.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

亽野灬性zι浪 2025-01-29 17:52:10

CSV文件中的字段名称不超过该文件的第一行。因此,在开始时,您可以简单地编写CSV文件中所需字段名称的行:

import csv

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

Field names in a csv file are no more than the first row of that file. So at the beginning you can simply write a row with the desired field names in your csv file:

import csv

with open('lat_lon.csv', 'w') as csvfile:
    writer = csv.writer(csvfile, delimiter=',')
    writer.writerow(['lat', 'lon'])
    # ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文