CSV data reader
CSV data reader reads files whose extension is .csv
.
[1]:
import tempfile
from pprint import pprint
from galactic.io.data.core import PopulationFactory
data = """\
age,height
46,202
36,183
204,230
36,180
23,150
23,172
"""
with tempfile.NamedTemporaryFile(mode="w+t", suffix=".csv") as file:
file.write(data)
file.seek(0)
population = PopulationFactory.create(file)
pprint({key: dict(value) for key, value in population.items()})
{'0': {'age': '46', 'height': '202'},
'1': {'age': '36', 'height': '183'},
'2': {'age': '204', 'height': '230'},
'3': {'age': '36', 'height': '180'},
'4': {'age': '23', 'height': '150'},
'5': {'age': '23', 'height': '172'}}
[2]:
data = """\
,age,height
id1,46,202
id2,36,183
id3,204,230
id4,36,180
id5,23,150
id6,23,172
"""
with tempfile.NamedTemporaryFile(mode="w+t", suffix=".csv") as file:
file.write(data)
file.seek(0)
population = PopulationFactory.create(file)
pprint({key: dict(value) for key, value in population.items()})
{'id1': {'age': '46', 'height': '202'},
'id2': {'age': '36', 'height': '183'},
'id3': {'age': '204', 'height': '230'},
'id4': {'age': '36', 'height': '180'},
'id5': {'age': '23', 'height': '150'},
'id6': {'age': '23', 'height': '172'}}