dimanche 10 juillet 2016

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!


Aucun commentaire:

Enregistrer un commentaire