I have a file that looks like this, I want to get data from this file so that I can create a dictionary that takes the neighboruhood names as keys, with its values being ID, Population, and Low rate.
ID Neighbourhood Name Population Low rate
1 East Billin-Pickering 43567 1000
2 North Eglinton-Silverstone-Ajax 33098 9087
3 Wistle-dong Lock 25009 6754
4 Squarion-Staion 10000 8790
5 Doki doki lolo 6789 2315
The output should look something like this (IF THE DICTIONARY I AM GIVEN IS EMPTY)
{'East Billin-Pickering':
{'id': 1, 'population': 43567, 'low_rate': 1000,
'North Eglinton-Silverstone-Ajax':
{'id': 2, 'population': 33098, 'low_rate': 9087},
'Wistle-dong Lock':
{'id': 3, 'population': 25009, 'low_rate': 6754},
'Squarion-Staion':
{'id': 4, 'population': 10000, 'low_rate': 8790},
'Doki doki lolo':
{'id': 5, 'population': 6789, 'low_rate': 2315}}
The file is already open, and I am also given a dictionary that may or may not have given values in it. How would I update that dictionary using data from the file?
Can somebody give me hints? I'm so confused.
I have no idea on how to start this. I know I have loop through the file, and will have to use strip() and split() methods at one point. I'm not sure how to actually get the values themselves, and modify them into a dictionary.
CodePudding user response:
If you can install 3rd party library you can use pandas as following:
import pandas as pd
data = pd.read_csv("test.csv", delimiter="\t") # Set delimiter and file name to your specific file
data = data.set_index("Neighbourhood Name")
final_dict = data.to_dict(orient="index")
Final dict now contains:
{
"East Billin-Pickering":{
"ID":1,
"Population":43567,
"Low rate ":1000
},
"North Eglinton-Silverstone-Ajax":{
"ID":2,
"Population":33098,
"Low rate ":9087
},
"Wistle-dong Lock":{
"ID":3,
"Population":25009,
"Low rate ":6754
},
"Squarion-Staion":{
"ID":4,
"Population":10000,
"Low rate ":8790
},
"Doki doki lolo":{
"ID":5,
"Population":6789,
"Low rate ":2315
}
}
CodePudding user response:
With pandas:
import pandas as pd
filename = 'myCSV.csv'
def read_csv(filename):
return pd.read_csv(filename).to_dict('records')
CodePudding user response:
You can use csv
and DictReader
. For example:
import csv
with open('input.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
print(row)
would print:
{'ID': '1', 'Neighbourhood Name': 'East Billin-Pickering', 'Population': '43567', 'Low rate': '1000'}
{'ID': '2', 'Neighbourhood Name': 'North Eglinton-Silverstone-Ajax', 'Population': '33098', 'Low rate': '9087'}
{'ID': '3', 'Neighbourhood Name': 'Wistle-dong Lock', 'Population': '25009', 'Low rate': '6754'}
{'ID': '4', 'Neighbourhood Name': 'Squarion-Staion', 'Population': '10000', 'Low rate': '8790'}
{'ID': '5', 'Neighbourhood Name': 'Doki doki lolo', 'Population': '6789', 'Low rate': '2315'}
So, with that you can construct any dictionary you want. In your specific case it could look something like:
import csv
result_dict = {}
with open('input.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
result_dict[row["Neighbourhood Name"]] = {
"ID": row.get("id"),
"population": row.get("population"),
"low_rate": row.get("low_rate")
}
That should give you the dictionary you wanted. Hope this helps.