zoukankan      html  css  js  c++  java
  • python 中 str与bytes的转换

    # bytes转字符串方式一
    b=b'xe9x80x86xe7x81xab'
    string=str(b,'utf-8')
    print(string)
    
    # bytes转字符串方式二
    b=b'xe9x80x86xe7x81xab'
    string=b.decode() # 第一参数默认utf8,第二参数默认strict
    print(string)
    
    # bytes转字符串方式三
    b=b'xe9x80x86xe7x81hahaxab'
    string=b.decode('utf-8','ignore') # 忽略非法字符,用strict会抛出异常
    print(string)
    
    # bytes转字符串方式四
    b=b'xe9x80x86xe7x81hahaxab'
    string=b.decode('utf-8','replace') # 用?取代非法字符
    print(string)
    
    # 字符串转bytes方式一
    str1='逆火'
    b=bytes(str1, encoding='utf-8')
    print(b)
    
    # 字符串转bytes方式二
    b=str1.encode('utf-8')
    print(b)
  • 相关阅读:
    redis
    魔法方法
    vue
    bbs技术总结
    Linux
    爬虫
    路飞项目
    Django-rest framework框架
    Django框架
    WEB
  • 原文地址:https://www.cnblogs.com/yingchen/p/9851270.html
Copyright © 2011-2022 走看看