我有一个
".txt“
其中包含JSON数据的文件。我想用python读取这个文件,并将其转换为dataframe。
此文本文件中的数据如下所示:
{
"_id" : "116b244599862fd2200",
"met_id" : [
612019,
621295,
725,
622169,
640014,
250,
350,
640015,
613689,
650423
],
"id" : "104",
"name" : "Energy",
"label" : "Peer Group",
"display_type" : "Risky Peer Group",
"processed_time" : ISODate("2019-04-18T11:17:05Z")
}
我试着用
pd.read_json
函数,但它总是显示一个错误。我是JSON的新手,如何使用这个文本文件并将其加载到Python中?
发布于 2019-04-18 18:50:26
请检查此link
此外,"processed_time" : ISODate("2019-04-18T11:17:05Z")
也不是JSON格式。
我们可以在https://jsonlint.com/中进行检查
我添加了代码。
import pandas as pd
import json
with open('rr.txt') as f:
string = f.read()
# Remove 'ISODate(', ')' For correct, we can use regex
string = string.replace('ISODate(', '')
string = string.replace(')', '')
jsonData = json.loads(string)
print (pd.DataFrame(jsonData))
https://stackoverflow.com/questions/55752025
复制相似问题