-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcsv_example.py
More file actions
65 lines (45 loc) · 1.57 KB
/
csv_example.py
File metadata and controls
65 lines (45 loc) · 1.57 KB
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
#!/usr/bin/env python
"""
Example of how to save data as an CSV file with the CSV module
"""
import csv
outfilename = "add_book_data.csv"
# get the data from the py file
# csv format really only holds flat data well.
from add_book_data_flat import AddressBook
# create a csv file writing object
writer = csv.writer( open(outfilename, 'wb') )
# write the headers
# assume all data have the same keys
headers = AddressBook[0].keys()
writer.writerow ( headers )
for person in AddressBook:
row = [person[key] for key in headers]
writer.writerow(row)
del writer # to make sure the file gets closed
### see if it can be re-loaded.
# create a csv file reading object
reader = csv.reader( open(outfilename, 'rb') )
# read the headers
headers = reader.next() # it's an iterator -- so next() gives us the next row -- in this case, the first row
# build up the new version:
AddressBook2 = []
for row in reader:
AddressBook2.append(dict(zip(headers, row)))
del reader # to make sure the file is closed
#Check if they are the same
if AddressBook2 == AddressBook:
print "csv readr version is the same as the original"
## or use the built-in "DictReader":
# create a DictReader file reading object
reader = csv.DictReader( open(outfilename, 'rb') )
# no need to read the headers -- it will use the first row
# build up the new version:
AddressBook3 = []
for row in reader:
print "row:", row
AddressBook3.append(row)
del reader # to make sure the file is closed
#Check if they are the same
if AddressBook3 == AddressBook:
print "The DictReader one is the the same"