[英]How to write two lists of different length to column and row in csv file
I have two lists of different length: 我有两个不同长度的列表:
list1 = ['a']
list2 = [['apple','banana','grapes']]
What I want in the csv is the following: 我在csv中想要的是以下内容:
col1 col2
a apple, banana, grapes
So first I tried itertool
and izip
, but it seems to put only first element of list in the list2 in col2, so it looks like: 所以首先我尝试了
itertool
和izip
,但它似乎只将list2的第一个元素放在col2中的list2中,所以看起来像:
col1 col2
a apple
How can I get the version above, with complete list in list2 as rows? 如何获得上面的版本,list2中的完整列表为行?
I used the following code to store the above to csv: 我使用以下代码将上面的内容存储到csv:
import csv
from itertools import izip
with open('some.csv', 'wb') as f:
writer = csv.writer(f)
writer.writerows(izip(list1,sum(list2,[]))
izip
will chop the longer list to match the shorter one, so izip(['a'], ['a', 'b', 'c'])
actually gives ['a', 'a']
, that is where the problem comes from. izip
会砍掉较长的列表以匹配较短的列表,所以izip(['a'], ['a', 'b', 'c'])
实际上给出['a', 'a']
,这就是问题来自。
Also generally you would want to use str.join()
instead of sum()
to convert a list to a string. 通常,您也希望使用
str.join()
而不是sum()
将列表转换为字符串。
From the question I guess you want a csv file delimited by tabs. 从问题我想你想要一个由制表符分隔的csv文件。 To solve the problem, first convert
list2
to a list of strings: 要解决此问题,请先将
list2
转换为字符串列表:
>>> list2_str = [','.join(lst) for lst in list2]
['apple,banana,grapes']
Then zip list1
and list2_str
: 然后zip
list1
和list2_str
:
>>> list3 = zip(list1, list2_str)
[['a', 'apple,banana,grapes']]
Open a csv writer with 'excel-tab' dialect and write the rows: 使用'excel-tab'方言打开csv writer并写入行:
>>> writer = csv.writer(file, dialect='excel-tab')
>>> writer.writerows(list3)
If instead you want a csv file with ',' as delimiter, simply remove the dialect
parameter. 如果您想要一个带有','作为分隔符的csv文件,只需删除
dialect
参数即可。 Python will correctly quote the second column, producing Python将正确引用第二列,生成
a,"apple,banana,grapes"
in the csv file. 在csv文件中。
You could use pandas
doing something similar to the following: 你可以使用
pandas
做类似以下的事情:
list1 = ['a']
list2 = [['apple','banana','grapes']]
import pandas as pd
f = pd.DataFrame({'col1':list1, 'col2':list2})
f.to_csv('filename.csv', header=True, index=False)
As @ZdaR pointed out in the comments, commas are the default separator in .csv
files, thus, storing your strings separated by a comma might lead to confusion. 正如@ZdaR在注释中指出的那样,逗号是
.csv
文件中的默认分隔符,因此,用逗号分隔存储字符串可能会导致混淆。 Instead, you could store your strings separated by a semicolon. 相反,您可以用分号存储字符串。
list1 = ['a', 'b']
list2 = [['apple','banana','grapes'], ['foo', 'bar']]
# convert all your lists of strings to single strings separated by a semicolon
list3 = [";".join(li) for li in list2]
list3
looks then as follows: list3
看起来如下:
['apple;banana;grapes', 'foo;bar']
Now you can use your code: 现在您可以使用您的代码:
from itertools import izip
import csv
with open('some.csv', 'wb') as f:
writer = csv.writer(f)
writer.writerows(izip(list1, list3))
which gives you the following output 它给你以下输出
a apple;banana;grapes
b foo;bar
If you then want to read the file back in again you can do this easily using eg pandas: 如果您想再次阅读该文件,可以使用例如pandas轻松完成:
import pandas as pd
df = pd.read_csv('some.csv', header=None, names=['col1', 'col2'])
which gives you: 这给你:
col1 col2
0 a apple;banana;grapes
1 b foo;bar
The approach depends on if you really want only two columns or more? 该方法取决于您是否真的只需要两列或更多列?
If extra columns are required: 如果需要额外的列:
import csv
from itertools import izip
list1 = ['a', 'b']
list2 = [['apple','banana','grapes'], ['cherry']]
with open('some.csv', 'wb') as f:
writer = csv.writer(f)
writer.writerow(["col1", "col2"])
for i1, i2 in izip(list1, list2):
writer.writerow([i1] + i2)
This would give you: 这会给你:
col1,col2
a,apple,banana,grapes
b,cherry
If only two columns are needed, you could switch to using tab characters for your delimiters to allow the commas to separate the list2
entries: 如果只需要两列,则可以切换为使用分隔符的制表符,以允许逗号分隔
list2
条目:
import csv
from itertools import izip
list1 = ['a', 'b']
list2 = [['apple','banana','grapes'], ['cherry']]
with open('some.csv', 'wb') as f:
writer = csv.writer(f, delimiter='\t')
writer.writerow(["col1", "col2"])
for i1, i2 in izip(list1, list2):
writer.writerow([i1] + [', '.join(i2)])
This would give you: 这会给你:
col1 col2
a apple, banana, grapes
b cherry
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.