Reading CSV files
Reading with open
# open and print line by line
with open('mock.csv') as f:
for line in f:
print(line)
id,first_name,last_name,email,gender,ip_address
1,Finn,Burchmore,fburchmore0@epa.gov,Male,74.20.172.212
2,Coral,Grigorey,cgrigorey1@cdc.gov,Female,151.91.212.169
3,Harold,McCormack,hmccormack2@marketwatch.com,Male,72.36.41.150
4,Thacher,Woodvine,twoodvine3@marketwatch.com,Male,66.50.122.48
5,Querida,Allmann,qallmann4@people.com.cn,Female,236.145.83.165
6,Irving,Loughman,iloughman5@globo.com,Male,19.142.188.98
7,Meghann,Vittet,mvittet6@cyberchimps.com,Female,235.59.102.124
8,Wolfgang,Mishow,wmishow7@gnu.org,Male,189.36.37.25
9,Babbie,Reide,breide8@jiathis.com,Female,129.226.63.38
10,Carson,Vowdon,cvowdon9@slashdot.org,Male,51.228.124.19
with open('mock.csv') as f:
# use enumerate to get the index as you iterate
for index, line in enumerate(f):
print(index, line)
0 id,first_name,last_name,email,gender,ip_address
1 1,Finn,Burchmore,fburchmore0@epa.gov,Male,74.20.172.212
2 2,Coral,Grigorey,cgrigorey1@cdc.gov,Female,151.91.212.169
3 3,Harold,McCormack,hmccormack2@marketwatch.com,Male,72.36.41.150
4 4,Thacher,Woodvine,twoodvine3@marketwatch.com,Male,66.50.122.48
5 5,Querida,Allmann,qallmann4@people.com.cn,Female,236.145.83.165
6 6,Irving,Loughman,iloughman5@globo.com,Male,19.142.188.98
7 7,Meghann,Vittet,mvittet6@cyberchimps.com,Female,235.59.102.124
8 8,Wolfgang,Mishow,wmishow7@gnu.org,Male,189.36.37.25
9 9,Babbie,Reide,breide8@jiathis.com,Female,129.226.63.38
10 10,Carson,Vowdon,cvowdon9@slashdot.org,Male,51.228.124.19
index = 0
processed_header = False
with open('mock.csv') as f:
for line in f:
# handle the header differently
if processed_header == False:
print("header", index, line)
processed_header = True
else:
print("row", index, line)
index += 1
header 0 id,first_name,last_name,email,gender,ip_address
row 1 1,Finn,Burchmore,fburchmore0@epa.gov,Male,74.20.172.212
row 2 2,Coral,Grigorey,cgrigorey1@cdc.gov,Female,151.91.212.169
row 3 3,Harold,McCormack,hmccormack2@marketwatch.com,Male,72.36.41.150
row 4 4,Thacher,Woodvine,twoodvine3@marketwatch.com,Male,66.50.122.48
row 5 5,Querida,Allmann,qallmann4@people.com.cn,Female,236.145.83.165
row 6 6,Irving,Loughman,iloughman5@globo.com,Male,19.142.188.98
row 7 7,Meghann,Vittet,mvittet6@cyberchimps.com,Female,235.59.102.124
row 8 8,Wolfgang,Mishow,wmishow7@gnu.org,Male,189.36.37.25
row 9 9,Babbie,Reide,breide8@jiathis.com,Female,129.226.63.38
row 10 10,Carson,Vowdon,cvowdon9@slashdot.org,Male,51.228.124.19
with open('mock.csv') as f:
for index, line in enumerate(f):
# handle the header differently
if index == 0:
print("header", index, line)
else:
print("row", index, line)
header 0 id,first_name,last_name,email,gender,ip_address
row 1 1,Finn,Burchmore,fburchmore0@epa.gov,Male,74.20.172.212
row 2 2,Coral,Grigorey,cgrigorey1@cdc.gov,Female,151.91.212.169
row 3 3,Harold,McCormack,hmccormack2@marketwatch.com,Male,72.36.41.150
row 4 4,Thacher,Woodvine,twoodvine3@marketwatch.com,Male,66.50.122.48
row 5 5,Querida,Allmann,qallmann4@people.com.cn,Female,236.145.83.165
row 6 6,Irving,Loughman,iloughman5@globo.com,Male,19.142.188.98
row 7 7,Meghann,Vittet,mvittet6@cyberchimps.com,Female,235.59.102.124
row 8 8,Wolfgang,Mishow,wmishow7@gnu.org,Male,189.36.37.25
row 9 9,Babbie,Reide,breide8@jiathis.com,Female,129.226.63.38
row 10 10,Carson,Vowdon,cvowdon9@slashdot.org,Male,51.228.124.19
headers = []
with open('mock.csv') as f:
for index, line in enumerate(f):
line = line.strip()
if index == 0:
# split the headers
headers = line.split(',')
else:
pass
print('header fields', headers)
header fields ['id', 'first_name', 'last_name', 'email', 'gender', 'ip_address']
headers = []
rows = []
with open('mock.csv') as f:
for index, line in enumerate(f):
line = line.strip()
if index == 0:
headers = line.split(',')
else:
# append the rows
rows.append(line.split(','))
print('header fields', headers)
print('rows fields', rows)
header fields ['id', 'first_name', 'last_name', 'email', 'gender', 'ip_address']
rows fields [['1', 'Finn', 'Burchmore', 'fburchmore0@epa.gov', 'Male', '74.20.172.212'], ['2', 'Coral', 'Grigorey', 'cgrigorey1@cdc.gov', 'Female', '151.91.212.169'], ['3', 'Harold', 'McCormack', 'hmccormack2@marketwatch.com', 'Male', '72.36.41.150'], ['4', 'Thacher', 'Woodvine', 'twoodvine3@marketwatch.com', 'Male', '66.50.122.48'], ['5', 'Querida', 'Allmann', 'qallmann4@people.com.cn', 'Female', '236.145.83.165'], ['6', 'Irving', 'Loughman', 'iloughman5@globo.com', 'Male', '19.142.188.98'], ['7', 'Meghann', 'Vittet', 'mvittet6@cyberchimps.com', 'Female', '235.59.102.124'], ['8', 'Wolfgang', 'Mishow', 'wmishow7@gnu.org', 'Male', '189.36.37.25'], ['9', 'Babbie', 'Reide', 'breide8@jiathis.com', 'Female', '129.226.63.38'], ['10', 'Carson', 'Vowdon', 'cvowdon9@slashdot.org', 'Male', '51.228.124.19']]
Reading with csv
Reading and Writing CSV Files in Python
import csv
with open('mock.csv') as f:
reader = csv.reader(f, delimiter=',')
for row in reader:
print(row)
['id', 'first_name', 'last_name', 'email', 'gender', 'ip_address']
['1', 'Finn', 'Burchmore', 'fburchmore0@epa.gov', 'Male', '74.20.172.212']
['2', 'Coral', 'Grigorey', 'cgrigorey1@cdc.gov', 'Female', '151.91.212.169']
['3', 'Harold', 'McCormack', 'hmccormack2@marketwatch.com', 'Male', '72.36.41.150']
['4', 'Thacher', 'Woodvine', 'twoodvine3@marketwatch.com', 'Male', '66.50.122.48']
['5', 'Querida', 'Allmann', 'qallmann4@people.com.cn', 'Female', '236.145.83.165']
['6', 'Irving', 'Loughman', 'iloughman5@globo.com', 'Male', '19.142.188.98']
['7', 'Meghann', 'Vittet', 'mvittet6@cyberchimps.com', 'Female', '235.59.102.124']
['8', 'Wolfgang', 'Mishow', 'wmishow7@gnu.org', 'Male', '189.36.37.25']
['9', 'Babbie', 'Reide', 'breide8@jiathis.com', 'Female', '129.226.63.38']
['10', 'Carson', 'Vowdon', 'cvowdon9@slashdot.org', 'Male', '51.228.124.19']
with open('mock.csv') as f:
reader = csv.reader(f, delimiter=',')
for index, row in enumerate(reader):
if index == 0:
print(f'{", ".join(row)}')
else:
print(f'id: {row[0]} first_name: {row[1]} last_name: {row[2]} email: {row[3]}')
id, first_name, last_name, email, gender, ip_address
id: 1 first_name: Finn last_name: Burchmore email: fburchmore0@epa.gov
id: 2 first_name: Coral last_name: Grigorey email: cgrigorey1@cdc.gov
id: 3 first_name: Harold last_name: McCormack email: hmccormack2@marketwatch.com
id: 4 first_name: Thacher last_name: Woodvine email: twoodvine3@marketwatch.com
id: 5 first_name: Querida last_name: Allmann email: qallmann4@people.com.cn
id: 6 first_name: Irving last_name: Loughman email: iloughman5@globo.com
id: 7 first_name: Meghann last_name: Vittet email: mvittet6@cyberchimps.com
id: 8 first_name: Wolfgang last_name: Mishow email: wmishow7@gnu.org
id: 9 first_name: Babbie last_name: Reide email: breide8@jiathis.com
id: 10 first_name: Carson last_name: Vowdon email: cvowdon9@slashdot.org
Reading CSV Files Into a Dictionary With csv
with open('mock.csv') as f:
reader = csv.DictReader(f)
for index, row in enumerate(reader):
print(f'id: {row["id"]} first_name: {row["first_name"]} last_name: {row["last_name"]} email: {row["email"]}')
id: 1 first_name: Finn last_name: Burchmore email: fburchmore0@epa.gov
id: 2 first_name: Coral last_name: Grigorey email: cgrigorey1@cdc.gov
id: 3 first_name: Harold last_name: McCormack email: hmccormack2@marketwatch.com
id: 4 first_name: Thacher last_name: Woodvine email: twoodvine3@marketwatch.com
id: 5 first_name: Querida last_name: Allmann email: qallmann4@people.com.cn
id: 6 first_name: Irving last_name: Loughman email: iloughman5@globo.com
id: 7 first_name: Meghann last_name: Vittet email: mvittet6@cyberchimps.com
id: 8 first_name: Wolfgang last_name: Mishow email: wmishow7@gnu.org
id: 9 first_name: Babbie last_name: Reide email: breide8@jiathis.com
id: 10 first_name: Carson last_name: Vowdon email: cvowdon9@slashdot.org
Optional Python CSV reader Parameters
- delimiter specifies the character used to separate each field. The default is the comma (‘,’).
- quotechar specifies the character used to surround fields that contain the delimiter character. The default is a double quote (‘ “ ‘).
- escapechar specifies the character used to escape the delimiter character, in case quotes aren’t used. The default is no escape character.
Writing CSV Files With csv
with open('scores.csv', mode='w') as employee_file:
writer = csv.writer(employee_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerow(['2019-05-01', 'Pirates', 0, 'Cubs', 10])
writer.writerow(['2019-05-15', 'Reds', 7, 'Pirates', 0])
- If quoting is set to csv.QUOTE_MINIMAL, then .writerow() will quote fields only if they contain the delimiter or the quotechar. This is the default case.
- If quoting is set to csv.QUOTE_ALL, then .writerow() will quote all fields.
- If quoting is set to csv.QUOTE_NONNUMERIC, then .writerow() will quote all fields containing text data and convert all numeric fields to the float data type.
- If quoting is set to csv.QUOTE_NONE, then .writerow() will escape delimiters instead of quoting them. In this case, you also must provide a value for the escapechar optional parameter.
Writing CSV File From a Dictionary With csv
with open('scores.csv', mode='w') as csv_file:
fieldnames = ['date', 'home_team', 'home_score', 'away_team', 'away_score']
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'date': '2019-05-01', 'home_team': 'Pirates', 'home_score': 0, 'away_team': 'Cubs', 'away_score': 10})
writer.writerow({'date': '2019-05-15', 'home_team': 'Reds', 'home_score': 7, 'away_team': 'Pirates', 'away_score': 0})
Some Notes on typecasting
x = "1"
print(x + str(3))
13
x = "1"
n = float(x)
print(n + n)
print(n * 3)
2.0
3.0
x = "blah"
print(x)
x = 125
print(x)
blah
125