import csv
with open(path, 'rb') as csvfile:
record = csv.reader(csvfile, delimiter=',')
for row in record:
print '-> '.join(row)
Once you open the file, csv.reader()iterate over each line, where row is a list of the values in each column. For example, suppose a CSV like:
famous_people.csv
Ned Stark;Robb Stark;John Snow;
Tywin Lannister;Jaime Lannister;Tyrion Lannister;
To read it you could do it in the following way
import csv
with open('personajes_famosos.csv', 'rb') as csvfile:
record = csv.reader(csvfile, delimiter=';')
for row in record:
print 'Padre:{}. Hijo1: {}. Hijo2: {} '.format(*(i for i in row))
You would have as output:
Padre: Ned Stark. Hijo1: Robb Stark. Hijo2: John Snow
Padre: Tywin Lannister. Hijo1: Jaime Lannister. Hijo2: Tyrion Lannister
You have to use the Python CSV library:
Once you open the file,
csv.reader()
iterate over each line, where row is a list of the values in each column. For example, suppose a CSV like:famous_people.csv
To read it you could do it in the following way
You would have as output:
You can find much more information about CSVs in the official documentation