-
Notifications
You must be signed in to change notification settings - Fork 19
/
Reading_and_Writing_CSV_Files.py
98 lines (87 loc) · 3.6 KB
/
Reading_and_Writing_CSV_Files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""
Project for Week 3 of "Python Data Analysis".
Read and write CSV files using a dictionary of dictionaries.
Be sure to read the project description page for further information
about the expected behavior of the program.
"""
import csv
def read_csv_fieldnames(filename, separator, quote):
"""
Inputs:
filename - name of CSV file
separator - character that separates fields
quote - character used to optionally quote fields
Ouput:
A list of strings corresponding to the field names in
the given CSV file.
"""
with open(filename, "rt", newline='') as csvfile:
csvreader = csv.DictReader(csvfile,
delimiter=separator, # default ,
quotechar=quote, # default "
quoting=csv.QUOTE_MINIMAL)
return csvreader.fieldnames
def read_csv_as_list_dict(filename, separator, quote):
"""
Inputs:
filename - name of CSV file
separator - character that separates fields
quote - character used to optionally quote fields
Output:
Returns a list of dictionaries where each item in the list
corresponds to a row in the CSV file. The dictionaries in the
list map the field names to the field values for that row.
"""
table = []
with open(filename, "rt", newline='') as csvfile:
csvreader = csv.DictReader(csvfile,
delimiter=separator, # default ,
quotechar=quote, # default "
quoting=csv.QUOTE_MINIMAL)
for row in csvreader:
table.append(row)
return table
def read_csv_as_nested_dict(filename, keyfield, separator, quote):
"""
Inputs:
filename - name of CSV file
keyfield - field to use as key for rows
separator - character that separates fields
quote - character used to optionally quote fields
Output:
Returns a dictionary of dictionaries where the outer dictionary
maps the value in the key_field to the corresponding row in the
CSV file. The inner dictionaries map the field names to the
field values for that row.
"""
table = {}
with open(filename, "rt", newline='') as csvfile:
csvreader = csv.DictReader(csvfile,
delimiter=separator, # default ,
quotechar=quote, # default "
quoting=csv.QUOTE_MINIMAL)
for row in csvreader:
table[row[keyfield]] = row
return table
def write_csv_from_list_dict(filename, table, fieldnames, separator, quote):
"""
Inputs:
filename - name of CSV file
table - list of dictionaries containing the table to write
fieldnames - list of strings corresponding to the field names in order
separator - character that separates fields
quote - character used to optionally quote fields
Output:
Writes the table to a CSV file with the name filename, using the
given fieldnames. The CSV file should use the given separator and
quote characters. All non-numeric fields will be quoted.
"""
with open(filename, 'w', newline='') as csvfile:
csv_writer = csv.DictWriter(csvfile,
fieldnames=fieldnames,
delimiter=separator,
quotechar=quote,
quoting=csv.QUOTE_NONNUMERIC)
csv_writer.writeheader()
for row in table:
csv_writer.writerow(row)