Delete unicode prefix (u) from JSON file with Python

0

I'm trying to read a JSON file with Python 2.7. To do this, I read line by line and extract the fields that interest me.

But I'm having problems because I get the prefix unicode (u) in some fields.

{u'quote_count': 0, u'contributors': None, u'truncated': False, u'name': u'Pilar Espinosa'}

I have thought about replacing the character u' with ' with replace but it has given me problems in some chains.

How could I remove it in another way?

    
asked by Pedro 26.12.2017 в 14:49
source

1 answer

0
{u'quote_count': 0, u'contributors': None, u'truncated': False, u'name': u'Pilar Espinosa'}

That is not JSON (at least valid JSON). Anyone would say that it is simply the textual form of a dict of Python. Yes, very similar to JSON, but it is not the same.

Loading said dict is possible without having to read line by line. If we assume that the text is in a variable a

mydict = eval(a)

The evaluation is already responsible for loading text strings in unicode format automatically.

Of course and if one does not have control of the source from which the file comes, eval may not be appropriate, because it is actually executing code (everything is basically Python code)

    
answered by 27.12.2017 в 07:34