Read all columns from CSV file?
<p>I am trying to read in a CSV file and then take all values from each column and put into a separate list. I do not want the values by row. Since the CSV reader only allows to loop through the file once, I am using the seek() method to go back to the beginning and read the next column. Besides using a Dict mapping, is there a better way to do this?</p>

<pre><code>infile = open(fpath, "r")
reader = csv.reader(infile)    

NOUNS = [col[0] for col in reader]
infile.seek(0)  # &lt;-- set the iterator to beginning of the input file

VERBS = [col[1] for col in reader]
infile.seek(0)
ADJECTIVES = [col[2] for col in reader]
infile.seek(0) 
SENTENCES = [col[3] for col in reader]
</code></pre>

<p>I am not sure why you dont want to use dict mapping. This is what I end up doing</p>

<p><strong>Data</strong></p>

<pre><code>col1,col2,col3
val1,val2,val3
val4,val5,val6
</code></pre>

<p><strong>Code</strong></p>

<pre><code>import csv
d = dict()
with open("abc.text") as csv_file:
    reader =  csv.DictReader(csv_file)
    for row in reader:
        for key, value in row.items():
            if d.get(key) is None:
                d[key] = [value]
            else:
                d[key].append(value)

print d
{'col2': ['val2', 'val5'], 'col3': ['val3', 'val6'], 'col1': ['val1', 'val4']}
</code></pre>

python python-3.x csv list-comprehension