0

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')
4
  • That's a great start. Next: open a file called "name.py" for writing Commented Jan 17, 2022 at 10:51
  • thanks not sure if you are cynical? I need to create an actual code only one time, that stay there forever, like an actual file with text/code array. Commented Jan 17, 2022 at 10:58
  • Yes, and you said that code needs to be in "name.py". Do you know how to write to a file? If so, show it in your code. If you don't know, you might clarify in your question that you don't know how to open a file for writing. Commented Jan 17, 2022 at 10:59
  • Do you know how to get data from df and print the contents of df? If so, show it. If you don't know, please clarify in your question that you don't know how to get the data out of the dataframe. Commented Jan 17, 2022 at 11:01

2 Answers 2

1

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) 
Sign up to request clarification or add additional context in comments.

5 Comments

worked! that's beautiful. But it is in a single row, how can I make it more readable? like having each dic in a single line of code?
as I assume you want this to be valid Python, arbitrarily inserting line breaks is dangerous. You can do things like printing each dictionary {...} on a separate line but not much beyond that
sorry for being dumb but how can I print each dic in each line? I will have to loop every line of this file? can you show?
pls see my edit
yea made it at the end, thanks so much for your time.
0
  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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.