美文网首页
2019-09-17 python2 unicode str

2019-09-17 python2 unicode str

作者: 窥识 | 来源:发表于2019-10-02 13:51 被阅读0次

    Python2.X如何将Unicode中文字符串转换成 string字符串

    普通字符串可以用多种方式编码成Unicode字符串,具体要看你究竟选择了哪种编码:

    unicodestring = u"Hello world"

    # 将Unicode转化为普通Python字符串:"encode"

    utf8string = unicodestring.encode("utf-8")

    asciistring = unicodestring.encode("ascii")

    isostring = unicodestring.encode("ISO-8859-1")

    utf16string = unicodestring.encode("utf-16")

    # 将普通Python字符串转化为Unicode:"decode"

    plainstring1 = unicode(utf8string, "utf-8")

    plainstring2 = unicode(asciistring, "ascii")

    plainstring3 = unicode(isostring, "ISO-8859-1")

    plainstring4 = unicode(utf16string, "utf-16")

    assert plainstring1 == plainstring2 == plainstring3 == plainstring4

    相关文章

      网友评论

          本文标题:2019-09-17 python2 unicode str

          本文链接:https://www.haomeiwen.com/subject/fdmcuctx.html