Python, pandas and json_read with utf-8 encoding

来源:互联网 时间:1970-01-01

The pandas library is a fantastic python toolkit to work with data. Recently I neededto read some json files in a pandas dataframe. Usually you can do that easily with the built in method:


import pandas as pdpd.read_json('example.json') importpandasaspdpd.read_json('example.json')

But this method fails, if it encounters utf-8 encoded files. In contrast to the more often used methods read_tableand read_csv, read_jsondoes not provide an encodingparameter. To work around this you have to import the codecs module and use the openmethod:


import codecsimport pandas as pdpd.read_json('example.json', 'r', 'utf-8')) importcodecsimportpandasaspdpd.read_json('example.json','r','utf-8'))