python csv写入两列
我正在尝试将 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
csv实际上只是一个带有CommaS生成V值的文件。所以从技术上来说:
应该可以解决问题。其中 value1 和 value2 是 2 个值的名称。
A csv is literally just a file with Comma Seperated Values. So technically:
should do the trick. Where value1 and value2 are the name of your 2 values.
你能试试这个吗?
你也可以尝试
Can you try this .
you can also try
只是一个想法,但你应该尝试:
Just an idea, but you should try to :
这对我有用:
This is what worked for me: