I'm trying to write a pandas DataFrame containing unicode to json, but the built in .to_json
function escapes the characters. How do I fix this?
Example:
import pandas as pd df = pd.DataFrame([['τ', 'a', 1], ['π', 'b', 2]]) df.to_json('df.json')
This gives:
{"0":{"0":"\u03c4","1":"\u03c0"},"1":{"0":"a","1":"b"},"2":{"0":1,"1":2}}
Which differs from the desired result:
{"0":{"0":"τ","1":"π"},"1":{"0":"a","1":"b"},"2":{"0":1,"1":2}}
force_ascii=False
argument: import pandas as pd df = pd.DataFrame([['τ', 'a', 1], ['π', 'b', 2]]) df.to_json('df.json', force_ascii=False)
But this gives the following error:
UnicodeEncodeError: 'charmap' codec can't encode character '\u03c4' in position 11: character maps to <undefined>
To convert the object to a JSON string, then use the Pandas DataFrame. to_json() function. Pandas to_json() is an inbuilt DataFrame function that converts the object to a JSON string. To export pandas DataFrame to a JSON file, then use the to_json() function.
orient: String value, ('dict', 'list', 'series', 'split', 'records', 'index') Defines which dtype to convert Columns(series into). For example, 'list' would return a dictionary of lists with Key=Column name and Value=List (Converted series).
This package contains a function, json_normalize. It will take a json-like structure and convert it to a map object which returns dicts. Output dicts will have their path joined by ".", this can of course be customized.
Opening a file with the encoding set to utf-8, and then passing that file to the .to_json
function fixes the problem:
with open('df.json', 'w', encoding='utf-8') as file: df.to_json(file, force_ascii=False)
gives the correct:
{"0":{"0":"τ","1":"π"},"1":{"0":"a","1":"b"},"2":{"0":1,"1":2}}
Note: it does still require the force_ascii=False
argument.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With