baltiturg
baltiturg

Reputation: 366

Create a python file with hardcoded array from a CSV file?

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

Answers (2)

piterbarg
piterbarg

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)

Edit

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

Mohamed Mostafa
Mohamed Mostafa

Reputation: 620

  1. you can return a generator of the rows you have in your dataframe using iterrows() method (you can check the document here)
data_generator = df.iterrows()
  1. Then you can convert it to a list of tuples (index, row)
data_list = list(data_generator)
  1. Then extract the rows only from your list of tuples and it will be on pandas.Series format (you can check the document from here)
rows = [item[1] for item in data_list]
  1. Then convert the pandas Series row to a dictionary
data = [item.to_dict() for item in rows]

Upvotes: 0

Related Questions