How to avoid decoding bytes to string all the time?

≡放荡痞女 提交于 2019-12-13 18:19:06

问题


There are some packages that returns values as bytes in my project. There is some configuration or environment variable to set so I don't need to decode bytes to string ever again? And if so, what is it?


回答1:


Python 2 by default can do what you want.

But let me advise: this is NOT what one really wants and that's why Python 3 does not do that automatically.

To convert bytes to str, you need to know the coding of the bytes:

s = b.decode(coding)

To convert str to bytes, you also need to know the desired coding:

b = s.encode(coding)

Python 2 assumed coding == 'ASCII' and thus worked for english / plain ASCII texts, but raised exceptions at runtime for everything else.

So, what you have to do is:

  • decide whether something should be processed as text (in that case you use str) or as binary (then you keep bytes)
  • decode early (after loading, receiving the bytes)
  • process as str
  • encode late (before saving, sending the bytes)

Nowadays utf-8 encoding is the most popular, so use that if you have no other requirements.




回答2:


No, I don't believe so. It is up to the packages as to how they return values in their methods.



来源:https://stackoverflow.com/questions/51227323/how-to-avoid-decoding-bytes-to-string-all-the-time

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