본문 바로가기

python3

[json] pandas dataframe to json file / json file read, write, append

1. pandas dataframe -> json file

df.to_json("filename.json", default_handler=str)

df.to_json("filename.json", oriend='records', default_handler=str)

 

2. json file -> pandas dataframe

df = pd.read_json('filename.json')

** large file **

import sys

maxparse = sys.maxsize

while True:

    try:

        df = pd.read_json("filename.json", dtype=object)

        break

    except OverflowError:

        maxparse = int(maxparse/10)

 

3. json file

with open("filename.json", mode='r', encoding='utf-8') as file:

    data = json.load(file)

 

with open("filename.json", mode='w', encoding='utf-8') as file:

    merged = data + temp

    file.seek(0)

    json.dump(merged, file)

 

with open("filename.json", mode='r+', encoding='utf-8') as file:

    data = json.load(file)

    merged = data + temp

    file.seek(0)

    json.dump(merged, file)