如何从python中的字典数据中删除unicode字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38074587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:18:01  来源:igfitidea点击:

How to remove unicode characters from Dictionary data in python

pythonjsonunicode

提问by Maverick

After using request library , am getting below dict in response.json()

使用请求库后,我在 response.json() 中低于 dict

{u'xyz': {u'key1': None, u'key2': u'Value2'}}

I want to remove all unicode characters and print only key value pairs without unicode chars

我想删除所有 unicode 字符并只打印没有 unicode 字符的键值对

I have tried below method to remove , but it shows malformed string

我尝试了以下方法来删除,但它显示格式错误的字符串

>>> import json, ast
>>> c = {u'xyz': {u'key1': None,u'key2': u'Value2'}}
>>> ast.literal_eval(json.dumps(c))

Getting 'ValueError: malformed string'

获取“ValueError:格式错误的字符串”

Any suggestion on how to do it ?

关于如何做到这一点的任何建议?

采纳答案by Ohad the Lad

Change your None to 'None':

将您的“无”更改为“无”:

 c = {u'xyz': {u'key1': 'None', u'key2': u'Value2'}}

it is a casting issue - ast likes str's

这是一个铸造问题 - ast 喜欢 str 的

Also, maybe u want to change all None to empty str or 'None' str... See this thread : Python: most idiomatic way to convert None to empty string?with this code, i've changes the empty string to 'None':

另外,也许您想将所有 None 更改为空 str 或 'None' str ...请参阅此线程: Python:将 None 转换为空字符串的最惯用方法?使用此代码,我已将空字符串更改为“无”:

def xstr(s):
    if s is None:
        return 'None'
    return str(s)

回答by S.K. Venkat

This snippet will helps you to preserve the data without unicode prefix notation u:

此代码段将帮助您保留没有 unicode 前缀符号的数据u

>>> import json
>>> c = {u'xyz': {u'key1': u'Value1',u'key2': u'Value2'}}
>>> print c
{u'xyz': {u'key2': u'Value2', u'key1': u'Value1'}}
>>> d = eval(json.dumps(c))
>>> print d
{'xyz': {'key2': 'Value2', 'key1': 'Value1'}}

json.dumps()will convert the dict to string type and eval()will reverse it.

json.dumps()将 dict 转换为字符串类型,而eval()将反转它。

Note: key1 value has changed from None to 'value1' for testing purpose

注意:出于测试目的,key1 值已从 None 更改为“value1”

回答by Marichyasana

You can use unicodestring.encode("ascii","replace")

您可以使用 unicodestring.encode("ascii","replace")

>>> ustr=u'apple'
>>> ustr
u'apple'
>>> astr=ustr.encode("ascii","replace")
>>> astr
'apple'

回答by RemcoGerlich

I don't really understand why you want this. Your variable is a normal Python dict with normal Unicode strings, and they happen to be printed as u''to distinguish them from bytestrings, but that shouldn't matter for using them.

我真的不明白你为什么要这个。您的变量是带有普通 Unicode 字符串的普通 Python dict,它们碰巧被打印出来u''以将它们与字节串区分开来,但这对于使用它们来说无关紧要。

If you want to save them as strings to read them as data later, JSON is a fine format for that. So no need to call request's .json()function at all, just use the response's .textattribute -- it's already JSON, after all.

如果您想将它们保存为字符串以便稍后将它们作为数据读取,那么 JSON 是一种很好的格式。所以根本不需要调用请求的.json()函数,只需使用响应的.text属性——毕竟它已经是 JSON。

Your try

你的尝试

>>> ast.literal_eval(json.dumps(c))

Fails because you first turn cinto JSON again, and then try to parse it as a Python literal. That doesn't work because Python isn't JSON; in particular one has nulland the other has None.

失败,因为您首先再次c转换为 JSON,然后尝试将其解析为 Python 文字。这是行不通的,因为 Python 不是 JSON;特别是一个有null,另一个有None

So maybe you want to change the Unicode strings into bytestrings? Like by encoding them as UTF8, that might work:

那么也许您想将 Unicode 字符串更改为字节字符串?就像将它们编码为 UTF8 一样,这可能会起作用:

def to_utf8(d):
    if type(d) is dict:
        result = {}
        for key, value in d.items():
            result[to_utf8(key)] = to_utf8(value)
    elif type(d) is unicode:
        return d.encode('utf8')
    else:
        return d

Or something like that, but I don't know why you would need it.

或者类似的东西,但我不知道你为什么需要它。