在Linux中打开时,使用Python编写csv文件,是空的

问题描述 投票:0回答:1

我抄从列表中的值到CSV文件中像

有2只列出了再说

list1 =['a','b','c','d','e','f','g','h','i','j','k','l','m','n']
list2 = ['1','2','3','4','5','6','7','8','9','10','11','12','13']

和我使用下面的代码写2只列出了CSV文件

import csv
from itertools import izip
with open('output1.csv', 'wb') as f:
    writer = csv.writer(f)
    writer.writerows(izip(list1, list2))

当我执行上面的事情后,Linux开放源output1.csv,它是空的

但是当我打印出来,使用打印在python脚本(打开(“output1.csv”)。阅读())是印刷CSV文件属性格式的内容

你可以请让我知道为什么该文件是空的,当我在Linux打开它

python python-3.x python-2.7 csv
1个回答
1
投票

当你被卡住接下来的时间,那么请具体谈谈你所期望,所以我们不必浪费时间假设你的预期输出的错误和输出。现在来到您的问题when i open output1.csv in linux after executing the above thing , it is empty我认为你需要阅读更多的关于iterators也关于file operations。所以运行在我的机器上你的脚本给了我这个错误,是很容易理解的。

TypeError: a bytes-like object is required, not 'str'

Python和它的错误是如此可读是吧!?这个错误是正确的,因为蟒蛇是期待一个类字节对象错误说!改变,要w+(和了解更多关于在答案中给出的链接的权限)。现在这个writer.writerows(izip(list1, list2))必须清楚地理解(也提供了链接在这个答案)。当生活给你一个迭代器,你只是重复了!抓住一个参考迭代的迭代器,你是好。请阅读更多关于它。

import csv
from itertools import izip

list1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n']
list2 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13']

with open('output1.csv', 'w+') as f:
    writer = csv.writer(f)
    writer.writerows([i for i in izip(list1, list2)]) # your homework to figure out what this means and does.

让我知道如果这个答案为你工作,接受它!

干杯!

© www.soinside.com 2019 - 2024. All rights reserved.