-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathrows.py
More file actions
43 lines (40 loc) · 1.11 KB
/
rows.py
File metadata and controls
43 lines (40 loc) · 1.11 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
# simple csv reader
# copyright (c) 2016 Tim Menzies http://menzies.us
# all rights reserved, BSD 2-clause license
from __future__ import division,print_function
import sys,string,re, math
sys.dont_write_bytecode=True
def rows(file,prep = None,
whitespace = '[\n\r\t]',
comments = '#.*',
sep = ","
):
"""
Walk down comma seperated values,
skipping bad white space and blank lines
"""
doomed = re.compile('(' + whitespace + '|' + comments + ')')
with open(file) as fs:
for line in fs:
line = re.sub(doomed, "", line)
if line:
row = map(lambda z:z.strip(), line.split(sep))
if len(row)> 0:
yield prep(row) if prep else row
def csv(file):
"""
Convert rows of strings to ints,floats, or strings
as appropriate
"""
def atoms(lst):
return map(atom,lst)
def atom(x) :
try: return int(x)
except:
try: return float(x)
except ValueError: return x
for row in rows(file, prep=atoms):
yield row
if __name__ == '__main__':
for row in csv('../data/weather.csv'):
print(row)