增加 Python str 与 bytes 之间的转换.

Signed-off-by: rick.chan <chenyang@autoai.com>
This commit is contained in:
rick.chan 2020-10-13 13:34:32 +08:00
parent e5cbc662cb
commit 74c4ad2522
1 changed files with 22 additions and 0 deletions

View File

@ -0,0 +1,22 @@
# Python str 与 bytes 之间的转换
```python
# bytes object
b = b"example"
# str object
s = "example"
# str to bytes
sb = bytes(s, encoding = "utf8")
# bytes to str
bs = str(b, encoding = "utf8")
# an alternative method
# str to bytes
sb2 = str.encode(s)
# bytes to str
bs2 = bytes.decode(b)
```