0

I hope to use the dictionary load by json file. However, each item contains the character 'u'. I need to remove the 'u's.

I tried dumps, but it does not work.

import ast
import json

data= {u'dot',
 u'dog',
 u'fog',
 u'eeee'}

res = eval(json.dumps(data))
print res

I hope to get: { 'dot', 'dog', 'fog, 'eeee' }

But the error is:

TypeError: set([u'eeee', u'fog', u'dog', u'dot']) is not JSON serializable

Enthus3d
  • 1,727
  • 12
  • 26

1 Answers1

2

The strings that start with u are unicode strings.

In your case, this has nothing to do with the problem:

data= {u'dot',
 u'dog',
 u'fog',
 u'eeee'}

This creates a set and stores the results in the data variable. The json serializer can't handle sets since the json spec makes no mention of them. If you change this to be a list, the serializer can handle the data:

res = set(eval(json.dumps(list(data))))

Here I'm converting the data variable to a list to serialize it, then converting it back to a set to store the result in the res variable.

Alternatively, you can directly ask Python to convert the unicode strings to strings, using something like this:

res = {x.encode("utf-8") for x in data}
print(res)
Anon Coward
  • 9,784
  • 3
  • 26
  • 37
  • Upvoted for mentioning the unicode conversion from python, as well as giving a detailed answer – Enthus3d Sep 15 '19 at 23:49
  • Thanks, it works well for set, when it changes to dictionary, it still not works well, for example:My_Dict = {u'animal': [u'dog', u'cat'], u'food': [u'rice', u'bread', u'milk'] } some_other_dict = json.loads(json.dumps(My_Dict)) print some_other_dict (res is:{u'food': [u'rice', u'bread', u'milk'], u'animal': [u'dog', u'cat']}) – askquestion628 Sep 15 '19 at 23:59
  • More complex types will require more work to parse. [Here's a simple function](https://pastebin.com/jRZ6Qtn1) to give you an idea of how to parse dicts, sets, or lists in one call. – Anon Coward Sep 16 '19 at 00:07