Reputation: 1857
I have this code which get me all the data from my .csv
file with DictReader
csv_path = "C:/Users/mydata.csv"
csv_database = open(csv_path, delimiters=";")
dataDict = csv.DictReader(csv_database, delimiter=";")
My database is like:
"Number0"; "date0"; "id0";...
"Number1"; "date1"; "id1";...
"Number2"; "date2"; "id2";...
...
And I want to get all the column in separate lists so they will look like :
[Number0, Number1, Number2,...]
[date0, date1, date2,...]
[id0, id1, id2,...]
Is there a way to do that, maybe using the headers which I can get with dataDict.fieldnames
?
Thank you for your help
Upvotes: 2
Views: 2553
Reputation: 785
zip(*[line.strip().split(',') for line in open(csv_path).readlines()])
Upvotes: 0
Reputation: 410
Well, I find a solution so here it is :
csv_path = "C:/Users/mydata.csv"
csv_database = open(csv_path, delimiters=";")
data_dict = csv.DictReader(csv_database, delimiter=";")
current_row = 0
number_list = []
for row in data_dict:
current_row += 1 # Skip heading row
if current_row == 1:
continue
number_list.append(row["Number"]) # Assuming the header of your column is "Number"
# Show results
for number in number_list:
print number
Upvotes: 2
Reputation: 1973
Take a look at numpy
You can do something like this with it:
>>> import numpy as np
>>> a = np.array([['hi','hey'],['sfg','asf']])
>>> a.transpose()
array([['hi', 'sfg'],
['hey', 'asf']],
dtype='|S3')
>>> print a.transpose()[1]
['hey' 'asf']
Upvotes: 1