function - Python: How to import information from a .csv file to python as a list with tuples inside? -
i new programming please excuse shallow knowledge coding. have .csv file can open excel. each row represents person's name , details (like address, phone number , age) each detail in different columns. whenever move onto new row person's detail.
i want import information python such each row (i.e. every detail of person) in 1 tuple (with each detail separated ',') , want tuples in list. list tuples inside them.
i have started coding opening file don't know how implement each detail detail of person in tuple , tuples in list. using python 2.7.
def load_friends(f): """ takes name of file containing friends information described in introduction , returns list containing information friends in file. load_friends(var) -> list """ openfile = open('friends.csv', 'ur') if f == 'friends.csv': openfile = open('friends.csv', 'ur') lines = openfile.readlines() print lines openfile.close()
with csv
module quite simple:
import csv open('friends.csv', 'ur') f: data = list(tuple(rec) rec in csv.reader(f, delimiter=','))
data
list of tuples.
the csv
module reads files correctly:
"smith, john",123
will read as
('smith, john', '123')
Comments
Post a Comment