如何将TXT文件中的JSON数据读入Pandas [英] How to read JSON data in TXT file into Pandas

查看:49
本文介绍了如何将TXT文件中的JSON数据读入Pandas的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个

".txt"

包含 JSON 数据的文件.我想在 python 中读取这个文件并将其转换为数据帧.

file which has JSON data in it. I want to read this file in python and convert it into a dataframe.

此文本文件中的数据如下所示:

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")
}

我尝试使用

pd.read_json

pd.read_json

函数,但它总是向我显示错误.我对 JSON 很陌生,如何使用此文本文件并在 Python 中加载它?

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?

推荐答案

请查看此链接

此外,"processed_time" : ISODate("2019-04-18T11:17:05Z") 不是 JSON 格式.
我们可以在 https://jsonlint.com/

Also, "processed_time" : ISODate("2019-04-18T11:17:05Z") is not JSON format.
We can check that in https://jsonlint.com/

我添加了 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))

这篇关于如何将TXT文件中的JSON数据读入Pandas的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆