dict、bytes類型轉換 bytes、str間類型轉換
bytes是二進制形式的字符串,
dict是可變容器(鍵值對)模型,且可存儲任意類型對象
方法1:
a={'Vod':'this is test'}}
r=bytes('{}'.format(a),'utf-8')
方法2:
map1 = {'aname':self.__ACCOUNT_NAME__}
b = json.dumps(map1).encode(encoding='utf-8')
由bytes轉dict用eval方法就可以
str和bytes相互轉換
# bytes object
b = b"simple"
# str object
s = "simple"
# str to bytes
bytes(s, encoding = "utf8")
# bytes to str
str(b, encoding = "utf-8")
# an alternative method
# str to bytes
str.encode(s)
# bytes to str
bytes.decode(b)
posted on 2021-03-20 12:56 Benjamin 閱讀(861) 評論(0) 編輯 收藏 引用 所屬分類: python

