python csv写入两列

发布于 2025-01-16 18:43:04 字数 429 浏览 3 评论 0原文

我正在尝试将 python 计算的输出写入两列中的 csv。我尝试了以下方法来实现此目的,但失败了。

import csv

with open('data.csv', 'ab') as f:
    writer = csv.writer(f)
    for row in [level, variable]:
        writer.writerow([row])

print(level,variable) 的打印输出如下,

(1, 0)
(2, 0)
---
---
---
(33, 8)
(34, 7)
(35, 6)
(1, 0)
(2, 0)
---
---
---
(33, 10)
(34, 1)
(35, 2)

如何将此打印输出写入 csv 文件的两列? 有谁能给个答案吗?提前致谢!

I am trying to write the output of a python calculation to csv in two columns. I tried the following to achieve this, but failed.

import csv

with open('data.csv', 'ab') as f:
    writer = csv.writer(f)
    for row in [level, variable]:
        writer.writerow([row])

the print output of print(level, variable) is as follows,

(1, 0)
(2, 0)
---
---
---
(33, 8)
(34, 7)
(35, 6)
(1, 0)
(2, 0)
---
---
---
(33, 10)
(34, 1)
(35, 2)

How can I write this print output in to two columns of csv file?
Can anyone give a answer? Thanks in advance!

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

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

发布评论

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

评论(4

奶茶白久 2025-01-23 18:43:04

csv实际上只是一个带有CommaS生成V值的文件。所以从技术上来说:

f = open("whatever.csv")
[for loop]
    f.write(f"{value1}, {value2}\n")
f.close()

应该可以解决问题。其中 value1 和 value2 是 2 个值的名称。

A csv is literally just a file with Comma Seperated Values. So technically:

f = open("whatever.csv")
[for loop]
    f.write(f"{value1}, {value2}\n")
f.close()

should do the trick. Where value1 and value2 are the name of your 2 values.

格子衫的從容 2025-01-23 18:43:04

你能试试这个吗?

  import csv
    
    with open('data.csv', 'w') as f:
        writer = csv.writer(f)
        writer.writerows(zip(level, variable))

你也可以尝试

f=open(data.csv,'w')
for i,j in zip(level,variable):
    f.write(str(i)+","+str(j))
f.close()

Can you try this .

  import csv
    
    with open('data.csv', 'w') as f:
        writer = csv.writer(f)
        writer.writerows(zip(level, variable))

you can also try

f=open(data.csv,'w')
for i,j in zip(level,variable):
    f.write(str(i)+","+str(j))
f.close()
忆沫 2025-01-23 18:43:04

只是一个想法,但你应该尝试:

  • 构建一个列表
  • 将你的元组放入列表中(python 用括号识别元组)
  • 在 csv 中发送元组列表

Just an idea, but you should try to :

  • Build a list
  • Put in your list your tuples (python identifies tuples with parenthesis)
  • Send your list of tuples in your csv
双手揣兜 2025-01-23 18:43:04

这对我有用:

import csv

with open(df_path,'w', newline='') as f:
    writer = csv.DictWriter(f, fieldnames=['level_col', 'variable_col'])
    writer.writeheader()
    for i in range(list_len):
        writer.writerow({'level_col': level[i], 'variable_col': variable[i]})

This is what worked for me:

import csv

with open(df_path,'w', newline='') as f:
    writer = csv.DictWriter(f, fieldnames=['level_col', 'variable_col'])
    writer.writeheader()
    for i in range(list_len):
        writer.writerow({'level_col': level[i], 'variable_col': variable[i]})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文