简体   繁体   中英

How to read JSON data in TXT file into Pandas

I have a

".txt"

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

I tried reading it using the

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?

Please check this link

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

I added python code.

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM