简体   繁体   中英

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']. 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. 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']". 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. The content appears more like a string literal version of a Python list. Given that you might be better off treating them as Python list literals, and parsing them with 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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