Skip to content

Instantly share code, notes, and snippets.

@lawrencechen0921
Created August 9, 2019 12:10
Show Gist options
  • Save lawrencechen0921/f04d53d59c53504af416262d4f9fe66f to your computer and use it in GitHub Desktop.
Save lawrencechen0921/f04d53d59c53504af416262d4f9fe66f to your computer and use it in GitHub Desktop.
使用unicode編碼與解碼
print('我是小帥哥'.encode('unicode-escape')) #將【我是一個小帥哥】以函式encode以 (unicode-escape)進行編碼
def utf8(data): #定義utf-8
data_byte=data.encode('utf-8') #回傳data的utf-8編碼結果指定給data_byte
data2=data_byte.decode('utf-8')
print('將',data,'經由utf-8編碼後為',data_byte)
print('將',data_byte,'經由utf-8解碼後為',data2)
print(data,'的長度為',len(data)) #將data長度顯示於螢幕上
print(data_byte,'的長度為',len(data_byte)) #將data_byte長度顯示於螢幕上
utf('我是一個小帥哥')
utf("b'\\u6211\\u662f\\u5c0f\\u5e25\\u54e5'")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment