Skip to content

Commit f413945

Browse files
committed
add basic csv exercise
1 parent 369cdc9 commit f413945

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

07-files/files6.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,22 @@
11

2-
# CSVs
2+
# CSVs, a basic guide; see
3+
# https://www.opentechguides.com/how-to/article/python/181/csv-read-write.html
4+
5+
import csv
6+
from pathlib import Path
7+
8+
filepath = Path('./data/VernonAuthority.csv')
9+
print(f"\nWorking with {filepath.absolute()}\n")
10+
11+
with open(filepath, mode='r', newline='') as auth_list:
12+
13+
dict_reader = csv.DictReader(auth_list)
14+
15+
dict_reader.fieldnames = [field.strip().lower().replace(' ', '_')
16+
for field in dict_reader.fieldnames]
17+
print(f"Headers: {'; '.join(dict_reader.fieldnames)}\n")
18+
19+
for row in dict_reader:
20+
print(row["system_id"] + "|" + row['name'] + "|" + row["long_name"])
21+
22+
# For a more fulsome explanation, see https://thispointer.com/?p=4309

0 commit comments

Comments
 (0)