Python CSV - Read, Write CSV in Python
Python CSV - Read, Write CSV in Python
Python CSV tutorial shows how to read and write CSV data with Python csv module.
CSV
CSV (Comma Separated Values) is a very popular import and export data format used in
spreadsheets and databases. Each line in a CSV file is a data record. Each record consists of one or
more fields, separated by commas. While CSV is a very simple data format, there can be many
differences, such as different delimiters, new lines, or quoting characters.
Method Description
csv.reader returns a reader object which iterates over lines of a CSV file
csv.writer returns a writer object which writes data into CSV file
csv.field_size_limit returns the current maximum field size allowed by the parser
$ cat numbers.csv
16,6,4,12,81,6,71,6
read_csv.py
#!/usr/bin/python3
import csv
f = open('numbers.csv', 'r')
with f:
reader = csv.reader(f)
In the code example, we open the numbers.csv for reading and read its contents.
reader = csv.reader(f)
$ ./read_csv.py
16
6
4
12
81
6
71
6
$ cat items.csv
pen|cup|bottle
chair|book|tablet
read_csv.py
#!/usr/bin/python3
import csv
f = open('items.csv', 'r')
with f:
The code example reads and displays data from a CSV file that uses a '|' delimiter.
$ ./read_csv2.py
pen
cup
bottle
chair
book
tablet
$ cat values.csv
min,avg,max
1, 5.5, 10
2, 3.5, 5
read_csv_dictionary.py
#!/usr/bin/python3
# read_csv3.py
import csv
f = open('values.csv', 'r')
with f:
reader = csv.DictReader(f)
The example reads the values from the values.csv file using the csv.DictReader.
Integramos la cienciacon
amor
The row is a Python dictionary and we reference the data with the keys.
write_csv.py
#!/usr/bin/python3
import csv
f = open('numbers2.csv', 'w')
with f:
writer = csv.writer(f)
The script writes numbers into the numbers2.csv file. The writerow() method writes a row of
data into the specified file.
$ cat numbers2.csv
1,2,3,4,5,6
7,8,9,10,11,12
It is possible to write all data in one shot. The writerows() method writes all given rows to the
CSV file.
write_csv2.py
#!/usr/bin/python3
import csv
f = open('numbers3.csv', 'w')
with f:
writer = csv.writer(f)
writer.writerows(nms)
The code example writes three rows of numbers into the file using the writerows() method.
write_csv_dictionary.py
#!/usr/bin/python3
import csv
f = open('names.csv', 'w')
with f:
writer.writeheader()
writer.writerow({'first_name' : 'John', 'last_name': 'Smith'})
writer.writerow({'first_name' : 'Robert', 'last_name': 'Brown'})
writer.writerow({'first_name' : 'Julia', 'last_name': 'Griffin'})
The example writes the values from Python dictionaries into the CSV file using the
csv.DictWriter.
New csv.DictWriter is created. The header names are passed to the fieldnames parameter.
writer.writeheader()
The writeheader() method writes the headers to the CSV file.
Integramos la cienciacon
amor
$ cat names.csv
first_name,last_name
John,Smith
Robert,Brown
Julia,Griffin
custom_dialect.py
#!/usr/bin/python3
import csv
csv.register_dialect("hashes", delimiter="#")
f = open('items3.csv', 'w')
with f:
The program uses a (#) character as a delimiter. The dialect is specified with the dialect option in
the csv.writer() method.
$ cat items3.csv
pens#4
plates#2
bottles#4
cups#1