Encoding a string to ascii

戏子无情 提交于 2019-12-22 03:54:33

问题


I have a long string that I want to encode to ascii. I'm doing:

s = s.encode('ascii', 'replace')

but I get:

'ascii' codec can't decode byte 0xc3 in position 2646: ordinal not in range(128)

(I've also tried 'ignore' but it doesn't help.)

What am I doing wrong?


回答1:


Your string is already encoded with some encoding. Before encoding it to ascii, you must decode it first.

Python is implicity trying to decode it (That's why you get a UnicodeDecodeError not UnicodeEncodeError).

You can solve the problem by explicity decoding your bytestring (using the appropriate encoding) before trying to reencode it to ascii.

Example:

s = s.decode('some_encoding').encode('ascii', 'replace')

Use the correct encoding your string was encoded in first place, instead of 'some_encoding'.

You have to know which encoding a string is using before you can decode it. Where did you get the string from?




回答2:


encode should be used on unicode objects to convert it to a str. If you have a str object, you should use decode to convert it to a unicode.



来源:https://stackoverflow.com/questions/1762564/encoding-a-string-to-ascii

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!