Monday, December 14, 2015

csv to list in python

This is dead simple way to read a csv file and feed the data into a Python list:

import csv
with open('file.csv', 'rb') as f:
    reader = csv.reader(f)
    your_list = list(reader)

print your_list
# [['This is the first line', 'Line1'],
#  ['This is the second line', 'Line2'],
#  ['This is the third line', 'Line3']]

Reference: http://stackoverflow.com/questions/24662571/python-import-csv-to-list