如何将XML中的中文字符转为unicode

2024-12-22 11:27:25
推荐回答(1个)
回答1:

普通字符串可以用多种方式编码成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