python - How to turn CSV file into list of rows? -


i have csv file saved windows comma separated values

f = open('file.csv')  csv_f = csv.reader(f)  row in lines:     print row 

returns

company,city,state  ciena corporation,linthicum,maryland  inspirage llc,gilbert,arizona  facebook,menlo park,ca 

i trying make list column column

f = open('file.csv') csv_f = csv.reader(f) row in f:     print row[2] f.close() 

and receiving 3rd letter: m, e, s , c.

writing in python on mac

as proper way dealing csv files can use csv module :

>>> import csv >>> open('f_name.csv', 'rb') csvfile: ...     spamreader = csv.reader(csvfile, delimiter=',') ...     row in spamreader: ...         print baseurl + ' '.join(row) 

this give rows list. , if want of them in list :

>>> import csv >>> open('f_name.csv', 'rb') csvfile: ...     spamreader = csv.reader(csvfile, delimiter=',') ...     print list(spamreader) 

note spamreader iterator , can convert list list() function.

you can list of columns passing spamreader zip function :

>>> import csv >>> open('f_name.csv', 'rb') csvfile: ...     spamreader = csv.reader(csvfile, delimiter=',') ...     print zip(*spamreader) 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -