简体   繁体   English

将清单清单写入CSV列

[英]Write list of lists to csv row

How would I print each list in list1 or list2 as a column without the '[ ]' included? 如何将列表1或列表2中的每个列表打印为不包含“ []”的列?

import csv
def test():
    list1 = [[1, 2, 3, 4], [3, 2, 5], [9, 3, 6, 8, 4]]
    list2 = [[2, 2, 4, 4], [1, 9, 10], [2, 7, 7, 4, 5]]
    with open('doc.csv', 'w', newline='') as file:
        writer = csv.writer(file, quotechar='"', quoting=csv.QUOTE_ALL)
        writer.writerow(list1)
        writer.writerow(list2)
if __name__ == '__main__':
    test()

This is close to what I desire but has [ ] in the string: 这接近我的要求,但字符串中包含[]:

[1, 2, 3, 4],[3, 2, 5],[9, 3, 6, 8, 4]
[2, 2, 4, 4],[1, 9, 10],[2, 7, 7, 4, 5]

EDIT: 编辑:

I want: 我想要:

"1, 2, 3, 4","3, 2, 5","9, 3, 6, 8, 4"
"2, 2, 4, 4","1, 9, 10","2, 7, 7, 4, 5"

A column is just printed by calling str on it. 只需在其上调用str即可打印一列。 So, if you want to do something else, you have to convert each column into a string yourself* (since calling str on a string just returns the string). 因此,如果您想做其他事情,则必须自己将每一列转换为一个字符串*(因为在字符串上调用str只会返回该字符串)。

In your case, we have to turn every element of the outer list into strings in the format we want… which will itself require turning every element of each inner list into strings so we can join them up in some way. 在您的情况下,我们必须将外部列表的每个元素转换为所需格式的字符串……这本身将需要将每个内部列表的每个元素转换为字符串,以便我们可以通过某种方式将它们连接起来。 In general, in Python, to do something to every element of a sequence, you either call map , or you write a comprehension, so I'll do one of each. 通常,在Python中,要对序列的每个元素进行处理,您可以调用map ,也可以编写一个理解,因此我将做一个。

After your edits, it looks like you want comma-separated columns, and comma-space-separated values within the columns, but that's OK because you want the columns auto-quoted. 编辑后,您似乎想要用逗号分隔的列,并希望在这些列中使用逗号分隔的值,但这没关系,因为您希望自动引用这些列。 That's easy. 这很容易。

To join up the values with ', ' , you just call ', '.join() on them. 要将值与', '连接起来,您只需在它们上调用', '.join()

def stringify_column_list(column_list):
    return ', '.join(str(value) for value in column_list)

The weird separator and quoting, you already know how to handle in the writer . 奇怪的分隔符和引号,您已经知道如何在writer处理。 So the only thing left is to call our formatting function on each column: 因此,剩下的唯一事情就是在每一列上调用我们的格式化函数:

writer.writerow(map(stringify_column_list, list1))
writer.writerow(map(stringify_column_list, list2))

You might even want to consider using the csv module again to create the column values. 您甚至可能要考虑再次使用csv模块来创建列值。 But this is probably overkill here. 但这在这里可能是矫kill过正。


* Or into some other object with a __str__ method that does what you want, but that's rarely worth doing. *或使用__str__方法进入您想要的其他对象,但这很少值得这样做。

This is how I'd do it: 这是我的方法:

import csv

def nested_list_formatter(nested):
    return tuple(','.join(str(item) for item in sublist) for sublist in nested)

def test():
    list1 = [[1, 2, 3, 4], [3, 2, 5], [9, 3, 6, 8, 4]]
    list2 = [[2, 2, 4, 4], [1, 9, 10], [2, 7, 7, 4, 5]]
    with open('doc.csv', 'w', newline='') as file:
        writer = csv.writer(file, quotechar='"', quoting=csv.QUOTE_ALL)
        writer.writerow(nested_list_formatter(list1))
        writer.writerow(nested_list_formatter(list2))

if __name__ == '__main__':
    test()

Resulting contents of doc.csv : doc.csv结果内容:

"1,2,3,4","3,2,5","9,3,6,8,4"
"2,2,4,4","1,9,10","2,7,7,4,5"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM