commit
8990494ab4
@ -115,7 +115,7 @@ kinds of input sources. For example:
|
||||
```python
|
||||
>>> import gzip
|
||||
>>> import stock
|
||||
>>> file = gzip.open('Data/portfolio.csv.gz')
|
||||
>>> file = gzip.open('Data/portfolio.csv.gz', 'rt')
|
||||
>>> port = reader.csv_as_instances(file, stock.Stock)
|
||||
>>> port
|
||||
[Stock('AA', 100, 32.2), Stock('IBM', 50, 91.1), Stock('CAT', 150, 83.44),
|
||||
|
@ -48,11 +48,10 @@ def read_csv_as_instances(filename, cls, *, headers=None):
|
||||
import csv
|
||||
|
||||
def convert_csv(lines, converter, *, headers=None):
|
||||
records = []
|
||||
rows = csv.reader(lines)
|
||||
if headers is None:
|
||||
headers = next(rows)
|
||||
return map(lambda row: converter(headers, row), rows)
|
||||
return list(map(lambda row: converter(headers, row), rows))
|
||||
```
|
||||
|
||||
|
||||
|
@ -9,12 +9,15 @@ def convert_csv(lines, converter, *, headers=None):
|
||||
return list(map(lambda row: converter(headers, row), rows))
|
||||
|
||||
def csv_as_dicts(lines, types, *, headers=None):
|
||||
return convert_csv(lines,
|
||||
lambda headers, row: { name: func(val) for name, func, val in zip(headers, types, row) })
|
||||
return convert_csv(lines,
|
||||
lambda headers, row: {name: func(val) for name, func, val in zip(headers, types, row)},
|
||||
headers=headers)
|
||||
|
||||
|
||||
def csv_as_instances(lines, cls, *, headers=None):
|
||||
return convert_csv(lines,
|
||||
lambda headers, row: cls.from_row(row))
|
||||
lambda headers, row: cls.from_row(row),
|
||||
headers=headers)
|
||||
|
||||
def read_csv_as_dicts(filename, types, *, headers=None):
|
||||
'''
|
||||
|
Loading…
Reference in New Issue
Block a user