FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonprog/code/live/reader.py at master · andybor/pythonprog · GitHub
andybor
/
pythonprog
Public
forked from
dabeaz/pythonprog
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonprog
/
code
/
live
/
reader.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
30 lines (27 loc) · 1.03 KB
Breadcrumbs
pythonprog
/
code
/
live
/
reader.py
Copy path
File metadata and controls
30 lines (27 loc) · 1.03 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
# reader.py
import
csv
def
read_csv
(
filename
,
types
,
*
,
errors
=
'warn'
):
'''
Read a CSV file with type conversion into a list of dicts
'''
if
errors
not
in
{
'warn'
,
'silent'
,
'raise'
}:
raise
ValueError
(
"errors must be one of 'warn', 'silent', 'raise'"
)
records
=
[]
with
open
(
filename
,
'r'
)
as
f
:
rows
=
csv
.
reader
(
f
)
headers
=
next
(
rows
)
# Skip the header row
for
rowno
,
row
in
enumerate
(
rows
,
start
=
1
):
try
:
row
=
[
func
(
val
)
for
func
,
val
in
zip
(
types
,
row
) ]
except
ValueError
as
err
:
if
errors
==
'warn'
:
print
(
'Row:'
,
rowno
,
'Bad row:'
,
row
)
print
(
'Row:'
,
rowno
,
'Reason:'
,
err
)
elif
errors
==
'raise'
:
raise
# Reraises the last exception
else
:
pass
# Ignore
continue
# Skips to the next row
record
=
dict
(
zip
(
headers
,
row
))
records
.
append
(
record
)
return
records
Back
|
FazBrowse Home
|
New Git URL