从列表创建逗号分隔的字符串[重复]

时间:2023-01-19 00:17:17

This question already has an answer here:

这个问题在这里已有答案:

I have a list of ints and want to create a comma-separated string. The following:

我有一个int列表,并希望创建一个逗号分隔的字符串。下列:

x = [3, 1, 4, 1, 5]
y = ",".join(x)

Give the error:

给出错误:

TypeError: sequence item 0: expected string, int found

How do I create the string? I could manually convert every element from int to string, insert this into a new list, and then do the join on this new list, but I'm wondering if there is a cleaner solution.

如何创建字符串?我可以手动将每个元素从int转换为字符串,将其插入到新列表中,然后在这个新列表上进行连接,但我想知道是否有更清晰的解决方案。

3 个解决方案

#1


10  

str.join only accepts an iterable of strings, not one of integers. From the docs:

str.join只接受一个可迭代的字符串,而不是一个整数。来自文档:

str.join(iterable)

Return a string which is the concatenation of the strings in the iterable iterable.

返回一个字符串,它是可迭代迭代中字符串的串联。

Thus, you need to convert the items in x into strings. You can use either map or a list comprehension:

因此,您需要将x中的项目转换为字符串。您可以使用地图或列表理解:

x = [3, 1, 4, 1, 5]
y = ",".join(map(str, x))

x = [3, 1, 4, 1, 5]
y = ",".join([str(item) for item in x])

See a demonstration below:

请参阅下面的演示:

>>> x = [3, 1, 4, 1, 5]
>>>
>>> ",".join(map(str, x))
'3,1,4,1,5'
>>>
>>> ",".join([str(item) for item in x])
'3,1,4,1,5'
>>>

#2


2  

Just modify the argument to join so that each element is converted to a string:

只需修改要连接的参数,以便将每个元素转换为字符串:

x = [3, 1, 4, 1, 5]
y = ",".join(str(i) for i in x)

#3


2  

If you are dealing with a csv file that is something other than the trivial example here, please do remember you are better off using the csv module to read and write csv, since CSV can be surprisingly complex.

如果您正在处理的csv文件不是此处的简单示例,请记住您最好使用csv模块来读取和写入csv,因为CSV可能非常复杂。

Here is a write example:

这是一个写例子:

import csv

x = [['Header\n1', 'H 2', 'H 3', 'H, 4'],[1,2,3,4],[5,6,7,8],[9,10,11,12]]

with open('/tmp/test.csv', 'w') as fout:
    writer=csv.writer(fout)
    for l in x:
        writer.writerow(l)

Note the embedded comma in 'H, 4' and the embedded new line in 'Header\n1' that may trip up efforts to decode if not properly encoded.

请注意'H,4'中的嵌入式逗号和'Header \ n1'中的嵌入式新行,如果编码不正确,可能需要重新解码。

The file 'test.csv' that the csv module produces:

csv模块生成的文件'test.csv':

"Header
1",H 2,H 3,"H, 4"
1,2,3,4
5,6,7,8
9,10,11,12

Note the quoting around "Header\n1" and "H, 4"` in the file so that the csv is correctly decoded in the future.

请注意文件中“Header \ n1”和“H,4”`的引用,以便将来正确解码csv。

Now check you can read that back:

现在检查你可以读回来:

with open('/tmp/test.csv') as f:
    csv_in=[[int(e) if e.isdigit() else e for e in row] 
               for row in csv.reader(f)]

>>> csv_in==x
True

Just sayin' You are usually better off using the tools in the library for csv.

只是说'你通常最好使用库中的csv工具。

#1


10  

str.join only accepts an iterable of strings, not one of integers. From the docs:

str.join只接受一个可迭代的字符串,而不是一个整数。来自文档:

str.join(iterable)

Return a string which is the concatenation of the strings in the iterable iterable.

返回一个字符串,它是可迭代迭代中字符串的串联。

Thus, you need to convert the items in x into strings. You can use either map or a list comprehension:

因此,您需要将x中的项目转换为字符串。您可以使用地图或列表理解:

x = [3, 1, 4, 1, 5]
y = ",".join(map(str, x))

x = [3, 1, 4, 1, 5]
y = ",".join([str(item) for item in x])

See a demonstration below:

请参阅下面的演示:

>>> x = [3, 1, 4, 1, 5]
>>>
>>> ",".join(map(str, x))
'3,1,4,1,5'
>>>
>>> ",".join([str(item) for item in x])
'3,1,4,1,5'
>>>

#2


2  

Just modify the argument to join so that each element is converted to a string:

只需修改要连接的参数,以便将每个元素转换为字符串:

x = [3, 1, 4, 1, 5]
y = ",".join(str(i) for i in x)

#3


2  

If you are dealing with a csv file that is something other than the trivial example here, please do remember you are better off using the csv module to read and write csv, since CSV can be surprisingly complex.

如果您正在处理的csv文件不是此处的简单示例,请记住您最好使用csv模块来读取和写入csv,因为CSV可能非常复杂。

Here is a write example:

这是一个写例子:

import csv

x = [['Header\n1', 'H 2', 'H 3', 'H, 4'],[1,2,3,4],[5,6,7,8],[9,10,11,12]]

with open('/tmp/test.csv', 'w') as fout:
    writer=csv.writer(fout)
    for l in x:
        writer.writerow(l)

Note the embedded comma in 'H, 4' and the embedded new line in 'Header\n1' that may trip up efforts to decode if not properly encoded.

请注意'H,4'中的嵌入式逗号和'Header \ n1'中的嵌入式新行,如果编码不正确,可能需要重新解码。

The file 'test.csv' that the csv module produces:

csv模块生成的文件'test.csv':

"Header
1",H 2,H 3,"H, 4"
1,2,3,4
5,6,7,8
9,10,11,12

Note the quoting around "Header\n1" and "H, 4"` in the file so that the csv is correctly decoded in the future.

请注意文件中“Header \ n1”和“H,4”`的引用,以便将来正确解码csv。

Now check you can read that back:

现在检查你可以读回来:

with open('/tmp/test.csv') as f:
    csv_in=[[int(e) if e.isdigit() else e for e in row] 
               for row in csv.reader(f)]

>>> csv_in==x
True

Just sayin' You are usually better off using the tools in the library for csv.

只是说'你通常最好使用库中的csv工具。