Reputation: 366
I have a CSV
and I would like to create a python file from it with array of dics
inside.
Any search on how to do that bring me to using Pandas to create a dic in memory.
I need to create a physical file name.py
in my project, and inside to have a code:
data = [{key:value},{key:value},...] # from my csv. Can have any other structure like dics in dic
I can start with this :
df = pd.read_csv ('index/index.csv')
Upvotes: 0
Views: 418
Reputation: 8219
df.to_dict('records')
generates output in the format that you want, I think
so it could look like
with open('name.py', 'w') as f:
records = df.to_dict('records')
print(f'data = {records}', file = f)
to print each record on a separate line you can do something like
records = df.to_dict('records')
with open('name.py', 'w') as f:
print('data = [', file = f)
for record in records:
print(f' {record},', file = f)
print(']', file = f)
Upvotes: 1
Reputation: 620
iterrows()
method (you can check the document here)data_generator = df.iterrows()
data_list = list(data_generator)
rows = [item[1] for item in data_list]
data = [item.to_dict() for item in rows]
Upvotes: 0