Python, pandas and json_read with utf-8 encoding

来源:转载

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:

Python

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:

Python

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



分享给朋友:
您可能感兴趣的文章:
随机阅读: