python中bytes和str的转换

1. bytes --> str :

  bytes_data = b'message'
  # 方法一:
  str_data = str(bytes_data, encoding = 'utf-8')
  # 方法二:
  str_data = bytes_data.decode('utf-8')

2. str --> bytes :

  str_data = 'message'
  # 方法一:
  bytes_data = bytes(str_data, encoding = 'utf-8')
  # 方法二:
  bytes_data = str_data.encode('utf-8')
posted @ 2020-11-22 16:06  二越  阅读(3802)  评论(0)    收藏  举报