[英]Python Removing the encoded characters from a string
I have a json, for instance: 我有一个json,例如:
item = {"name": '\x84\xa2 Target', ...}
in a function that ends with: 以以下结尾的函数:
return json.dumps(item, ensure_ascii=True)
Running the function causes this error: 运行该函数会导致此错误:
UnicodeDecodeError: 'utf8' codec can't decode byte 0x84 in position 6: invalid start byte
UnicodeDecodeError:'utf8'编解码器无法解码位置6的字节0x84:无效的起始字节
I have tried 我努力了
return json.dumps(item, ensure_ascii=False).encode('utf-8')
But this gives the same error. 但这给出了相同的错误。
This code below does 'work', but the json that it gives out confuses other code down the road (not on my end): 下面的代码可以正常工作,但是它给出的json混淆了其他代码(不是我的意思):
return json.dumps(item, encoding="ISO-8859-1")
I would like to know how to just delete all 'complex' characters from any string. 我想知道如何从任何字符串中删除所有“复杂”字符。
This is stupid, but appears to work: 这很愚蠢,但似乎可以工作:
"".join([c for c in json.dumps(item, ensure_ascii=False) if c in string.printable])
From 从
item = {"name": '\x84\xa2 Target'}
it returns 它返回
'{"name": " Target"}'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.