Hello Guys ,
a colleague of mine collected some twitter data (name, follower, etc.). He uses MongoDB and sent me an csv-export. The typical "pandas.read_csv().." does not work.
The structure of the csv-file is as follows:
{ "_id ":{"$oid":"5cf683d18eb9ad12c84f6417"},"ID":"14400049","name":"Laura
This is my Code:
import csv
import json
csvFilePath = 'xxx'
jsonFilePath = 'yyy'
# read the csv and add the data to a dictionary..
data = {}
with open(csvFilePath, encoding="utf8") as csvFile:
csvReader = csv.DictReader(csvFile)
for rows in csvReader:
id = rows["_id"]
data[id] = rows
# create new json file and write data on it
with open(jsonFilePath, 'w') as jsonFile:
# make it more readeble and prette
jsonFile.write(json.dumps(data, indent=4))
I get an key error which means that the loop doesnt get rows["_id"]
Anybody there who can help me? Alternative solutions are also welcome. My goal is to load the data into a jupyter notebook.
Thank you guys very much in advance.
import pandas as pd
csvFilePath = 'xxx'
jsonFilePath = 'yyy'
csv = pd.read_csv(csvFilePath)
json = csv.to_json()
with open(jsonFilePath, 'w') as jsonFile:
jsonFile.write(json)
I suppose, pandas package can help you
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.