python3中的字符编码

bytes

1
2
3
4
5
6
7
8
9
10
>>> s = "中文"
>>> s
'中文'
>>> type(s)
<class 'str'>
>>> b = bytes(s, encoding='utf-8')
>>> b
b'xe4xb8xadxe6x96x87'
>>> type(b)
<class 'bytes'>

str

1
2
3
4
5
6
7
8
9
10
11
12
13
14
>>> b
b'xe4xb8xadxe6x96x87'
>>> type(b)
<class 'bytes'>
>>> s1 = str(b)
>>> s1
"b'\xe4\xb8\xad\xe6\x96\x87'"
>>> type(s1)
<class 'str'>
>>> s1 = str(b, encoding='utf-8')
>>> s1
'中文'
>>> type(s1)
<class 'str'>

获取当前路径

1
2
import os
print(os.getcwd())