I want python to store csv data in memory on my PC as a dictionary.

Asked 2 years ago, Updated 2 years ago, 100 views

Thank you for your patience.
I made csv in openw as below

import csv

data = {'hito':61, 'hiro':54, 'yuto':17, 'osamu':67, 'keiko':71}

with open('name.csv', 'w', newline=')ascsv_file:
    fieldnamse=['Name', 'Date']
    writer=csv.DictWriter(csv_file, fieldnames=fieldnamse)
    writer.writeheader()
    for name, key in data.items():
        writer.writerow({
        'Name': name,
        'Date': key
        })

The results are as follows.

Name, Date
hito, 61
hiro,54
yuto, 17
osamu,67
keiko, 71

Next time, I would like to store the data stored in csv in the memory of my PC.Virtual memory is also fine.
I did the following.

import csv
with open('name.csv', 'r') ascsv_file:
    reader=csv.DictReader(csv_file)
    print('Name', 'Date')
    d = {}
    For row in reader:
        print(row['Name'], row['Date'])

Now it's just a print statement, and row['Name'], row['Date'] is
Once in memory, it will be rewritten during Loop.

with open('name.csv', 'r')ascsv_file:
    reader=csv.DictReader(csv_file)
    print('Name', 'Date')
    d = {}
    For row in reader:
        A = row ['Name']
        B=row ['Date']
    return read_data`

The data stored in A and B are rewritten.
I'm searching books and the web for arrays, but I can't find them.
When I asked questions on other sites, I received only sophisticated answers.

read_data.append(row['Name']...)

It looks like , but append is not available.
Finally, return is considering the method of the image stored in read_data.
I'm just thinking about not using pandas in a dictionary format.

As a result, the above csv data will contain dictionary data and data, but the final goal is to process the data in the memory as requested and then change it to openw or r+.

I think I can manage it if I use the class, but I'm afraid it's going to be a big deal, so I've stopped working.
Thank you for your cooperation.
    

python csv

2022-09-30 19:55

3 Answers

For dictionary types, you can add and update keys and values as follows:

with open('test.csv', 'r')ascsv_file:
    reader=csv.DictReader(csv_file)
    d = {}
    For row in reader:
        d[row['Name']]=row['Date']

In the case of dictionary type, it is more compatible to use json than to use csv, and the next writing is

import json

data = {'hito':61, 'hiro':54, 'yuto':17, 'osamu':67, 'keiko':71}
with open('name.json', 'w') as f:
    json.dump(data,f)

Now you can load it.

import json

with open('name.json', 'r') as f:
    data=json.load(f)


2022-09-30 19:55

If you want to use one of Python's libraries, Pandas, you can write in one line.

import pandas aspd#If you get an error here, you need to install it.
pd.read_csv("name.csv").set_index("Name").to_dict()["Date"]

Pandas supports a class called DataFrame that can handle csv-like data in-memory, and it also supports interconversion with basic Python dictionaries, making it easy to do this.


2022-09-30 19:55

Thank you for your reply.

with open('test.csv', 'r')ascsv_file:
    reader=csv.DictReader(csv_file)
    d = {}
    For row in reader:
        d[row['Name']] = row['Date'] with open('test.csv', 'r') ascsv_file:

Finally, `
return
So, I was able to temporarily store it in memory.
I never thought it would be this easy.I wrote the code in the class a while ago and succeeded, but this one is much better.

Now, regarding json, I often see it in API data, and I'm surprised that it goes well with python.
However, the python 3.6.x installed on Win10 does not show dump().
It's amazing even though it's standard.

Thank you very much.`


2022-09-30 19:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.