- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_encode.py
More file actions
Latest commit
30 lines (27 loc) · 813 Bytes
/
Copy pathstring_encode.py
File metadata and controls
30 lines (27 loc) · 813 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 18 11:05:17 2017
@author: shengjiex@qq.com
encode 编码||decode 解码||encoding 格式
>>> the help about open?
open(file, mode='r', buffering=-1, encoding=None, \
errors=None, newline=None, closefd=True, opener=None)
"""
test_str='胜杰open file'
save=r'coding.txt'
withopen(save,'w',encoding='utf-8') asf:
f.write(test_str)
withopen(save,'r',encoding='gbk') asf:
print('encode by utf-8,decode by gbk : ',f.read())
#字符的编码
gbk_str=test_str.encode('gbk')
""">>>b'\xca\xa4\xbd\xdcopen file'"""
#字符的解码,错误
try:
gbk_str.decode('utf-8')
except:
pass
""">>>'utf-8' codec can't decode byte 0xbd in position 2: invalid start byte"""
#字符的解码,正确
gbk_str.decode('gbk')
""">>>'胜杰open file'"""