Python json.loads for utf-8

时间:2015-06-25 18:33:22

标签: python json utf-8

I have a (not valid) json file, which is in UTF-8 format

The rough sketch of the json is:

{u'key': {u'key2': u'value'}, ...., u'key3' : u'value'}

Doing a simple python json.loads() results in the following error :

ValueError: Expecting property name: line 1 column 2 (char 1)

Following some related answers on SO, I tried chanding it to unicode:

line = unicode(line,'utf-8')
data = json.loads(line)


ValueError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

One solution I can think is to replace all single quotes by double quotes and proceed, but I was thinking - if there's an easier solution to parse the file to get python dict?

1 个答案:

答案 0 :(得分:7)

This is NOT JSON! It looks like a python serialization through repr which you can load with ast.literal_eval(node_or_string) from the ast-module.