简体   繁体   English

CSV文件列中的对象是列表。 如何使用Python将它们组合成一个巨型列表?

[英]Objects in column of csv file are lists. How do I combine these into one giant list using Python?

I have a csv file whose columns consists entirely of lists like ['spam', 'eggs', ..., 'spam']. 我有一个csv文件,其列完全由[[spam],'eggs',...,'spam']等列表组成。 Ideally I want to aggregate all of these lists together into one giant list, but am running into trouble doing that; 理想情况下,我想将所有这些列表汇总到一个巨大的列表中,但是这样做很麻烦。 initially I thought something like: 最初我以为是这样的:

import csv
with open(<filepath>, 'r') as csvfile:
    reader = csv.reader(csvfile)
    lst = []
    for row in reader:
        lst += row

So my idea was basically that each "row in reader" would more-or-less be the list ['spam', 'eggs', ..., 'spam'] and that I could just combine these together one at a time, but that evidently ran into some problems. 所以我的想法基本上是,每个“读者行”或多或少都是列表['spam','eggs',...,'spam'],我可以一次将它们组合在一起,但显然会遇到一些问题。 Is there anyway to make this work? 反正有做这项工作吗? Any help is appreciated! 任何帮助表示赞赏!

Edit: To give more context, let's say this csv has one column only and three rows, each of which is literally "['spam', 'eggs', 'spam', 'eggs']". 编辑:为了提供更多的上下文,我们假设此csv只有一列和三行,每行实际上是“ ['spam','eggs','spam','eggs']”。 The desired output would be: 所需的输出将是:

"['spam', 'eggs', 'spam', 'eggs', 'spam', 'eggs', 'spam', 'eggs', 'spam', 'eggs', 'spam', 'eggs']"

But the output I'm getting from my code instead is: 但是我从代码中得到的输出是:

"[['spam', 'eggs', 'spam', 'eggs'], ['spam', 'eggs', 'spam', 'eggs'], ['spam', 'eggs', 'spam', 'eggs']]

So the question is around how to fix this kind of issue. 因此,问题在于如何解决此类问题。

A file containing ['spam', 'eggs', 'spam', 'eggs'] is not a CSV file. 包含['spam', 'eggs', 'spam', 'eggs']的文件不是CSV文件。 The content appears more like a string literal version of a Python list. 内容看起来更像是Python列表的字符串文字版本。 Given that you might be better off treating them as Python list literals, and parsing them with ast.literal_eval() : 鉴于您最好将它们视为Python列表文字,并使用ast.literal_eval()解析:

from ast import literal_eval

with open(<filepath>) as infile:
    lst = []
    for line in infile:
        lst.extend(literal_eval(line))

If your input file contains this: 如果您的输入文件包含以下内容:

['spam', 'eggs', 'spam', 'eggs']
['more', 'spam', 'more', 'eggs']
['spam', 'spam', 'spam', 'spam']

lst would end up containing this: lst最终将包含此:

['spam', 'eggs', 'spam', 'eggs', 'more', 'spam', 'more', 'eggs', 'spam', 'spam', 'spam', 'spam']

暂无
暂无

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

相关问题 如果我有一个CSV文件的Python列表,如何将它们全部合并为一个巨型CSV文件? - If I have a Python list of CSV files, how do I merge them all into one giant CSV file? CSV 列有列表列表。 如何将此列转换为具有 sqlite 的关系数据库? - CSV column has list of lists. How do I turn this column into a relational database with sqlite? 使用两个列表写入csvFile。 如何将每个列表写到两个不同的列 - Writing to a csvFile using two lists. How do i write each list to two different columns Python —将带龟的图形与嵌套列表一起使用。 我该如何解决? - Python — Using turtle graphics with nested lists. How do I solve this? 使用熊猫,如何将一个 csv 文件列转换为列表,然后使用创建的列表过滤不同的 csv? - Using pandas, how do I turn one csv file column into list and then filter a different csv with the created list? 使用json和列表列表。 如何在文件中循环添加列表并稍后将其加载为列表列表 - Using json with list of lists. How to append lists in loop in file and later load it as list of lists 如何将 dataframe 列中的列表合并为一个列表 - How do I combine lists in column of dataframe to a single list 在 Python 中,如何高效地对 map 巨表列出对象? - In Python, how to map giant list to objects efficiently? Pandas - Dataframe 具有带列表的列。 如何对列表中的元素进行分组? - Pandas - Dataframe has column with lists. How can I groupby the elements within the list? 如何确定列表列表中列表的大多数出现。 (Python) - How to determine the majority of appearances of a list in list of lists. (Python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM