简体   繁体   中英

Python Removing the encoded characters from a string

I have a json, for instance:

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

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):

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"}'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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