[英]How to read JSON data in TXT file into Pandas
I have a 我有一个
".txt"
“。文本”
file which has JSON data in it. 其中包含JSON数据的文件。 I want to read this file in python and convert it into a dataframe.
我想在python中读取这个文件并将其转换为数据帧。
The Data in this text file looks like this: 此文本文件中的数据如下所示:
{
"_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")
}
I tried reading it using the 我尝试用它来阅读它
pd.read_json
pd.read_json
function but it always shows me an error. 功能,但它总是显示一个错误。 I am quite new to JSON, how can I use this Text file and load it in Python?
我是JSON的新手,如何使用这个Text文件并在Python中加载它?
Also, "processed_time" : ISODate("2019-04-18T11:17:05Z")
is not JSON format. 另外,
"processed_time" : ISODate("2019-04-18T11:17:05Z")
不是JSON格式。
We can check that in https://jsonlint.com/ 我们可以在https://jsonlint.com/查看
I added python code. 我添加了python代码。
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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.