简体   繁体   中英

UnicodeDecodeError in a pandas dataframe created from JSON file

I have a piece of code running on an iPython notebook that downloads a JSON file and then parses the content into a Pandas DF. However, if I try to inspect the DF, then I get an encoding error.

output = r.json()
columns_map = {'/people/person/date_of_birth': 'birth_date',
              '/people/person/place_of_birth': 'birth_place',
              '/people/person/gender': 'gender'}
dF = pd.DataFrame(output['result'])
dF.rename(columns=columns_map, inplace=True)
dF.to_csv('file.csv',encoding='utf-8')

I can create a CSV from the DF w/o any problems, but If i type

dF

To inspect the dF from inside the iPython notebook, I get this:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1894: ordinal not in range(128)

Can anybody help?

After some research, I found that this is a problem with Python version < 3.0. For some weird reason, the quick fix is to import sys and relaod sys. This worked for me:

import sys    
reload(sys)  
sys.setdefaultencoding('utf8')

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