简体   繁体   English

从字典python中删除u键

[英]Remove u key from dictionary python

How to exclude the key 'u' from below, 如何从下面排除键“ u”,

{u'{"auth":{"user_id":"2"},"data":{"collection":"master-services"}}': [u'']}

I need to get my dictionary like below, 我需要得到如下的字典,

{"auth":{"user_id":"2"},"data":{"collection":"master-services"}}

It looks like you have a dictionary where the key(s) is JSON data. 好像您有一个字典,其中的键是JSON数据。 Try parsing it with a JSON parser. 尝试使用JSON解析器进行解析。

>>> json.loads(list(data)[0])
{'auth': {'user_id': '2'}, 'data': {'collection': 'master-services'}}

If you have many such keys, you can iterate over data (or, data.keys() ), like this: 如果您有很多这样的键,则可以遍历data (或data.keys() ),如下所示:

>>> new_data = [json.loads(d) for d in data]

This gives you a list of dictionaries. 这为您提供了词典列表。

u stands for Unicode Text. u代表Unicode文本。 It is used for creating Unicode strings. 它用于创建Unicode字符串。 It is not a character that's stored in the dictionary. 字典中存储的不是字符。

You just need the key of the dictionary entry. 您只需要字典条目的键。 Because there is only one key, you can do this: 因为只有一个键,所以您可以执行以下操作:

my_dict = {u'{"auth":{"user_id":"2"},"data":{"collection":"master-services"}}': [u'']}
my_key = next(iter(my_dict))

my_key will hold the value {"auth":{"user_id":"2"},"data":{"collection":"master-services"}} my_key将保存值{"auth":{"user_id":"2"},"data":{"collection":"master-services"}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM