Skip to content

Commit 47368e9

Browse files
committed
3.7
1 parent ab9a126 commit 47368e9

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

Work/fileparse.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55

66

77
def parse_csv(
8-
filename: str, select: list = [], types: list = [], has_headers: bool = True
8+
filename: str,
9+
select: list = [],
10+
types: list = [],
11+
has_headers: bool = True,
12+
delimiter: str = ",",
913
) -> list:
1014
"""
1115
Parse a CSV file into a list of records
1216
"""
1317
with open(filename) as f:
14-
rows = csv.reader(f)
18+
rows = csv.reader(f, delimiter=delimiter)
1519
if has_headers:
1620
orig_headers = next(rows)
1721
headers = select if select else orig_headers

Work/fileparse_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,15 @@ def test_parse_csv_no_headers_3_6():
7070
("WMT", 49.74),
7171
("XOM", 69.35),
7272
]
73+
74+
75+
def test_parse_csv_delimiter_3_7():
76+
assert parse_csv("Data/portfolio.dat", types=[str, int, float], delimiter=" ") == [
77+
{ "name": "AA", "shares": 100, "price": 32.2 },
78+
{ "name": "IBM", "shares": 50, "price": 91.1 },
79+
{ "name": "CAT", "shares": 150, "price": 83.44 },
80+
{ "name": "MSFT", "shares": 200, "price": 51.23 },
81+
{ "name": "GE", "shares": 95, "price": 40.37 },
82+
{ "name": "MSFT", "shares": 50, "price": 65.1 },
83+
{ "name": "IBM", "shares": 100, "price": 70.44 },
84+
]

0 commit comments

Comments
 (0)