本文介绍了如何用python3制作unicode字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用过这个:

u = unicode(text, 'utf-8')

但是在使用 Python 3 时出现错误(或者...也许我只是忘记包含某些内容):

But getting error with Python 3 (or... maybe I just forgot to include something) :

NameError: global name 'unicode' is not defined

谢谢.

推荐答案

文字字符串在 Python3 中默认为 unicode.

Literal strings are unicode by default in Python3.

假设 text 是一个 bytes 对象,只需使用 text.decode('utf-8')

Assuming that text is a bytes object, just use text.decode('utf-8')

unicode相当于Python3的str,所以你也可以这样写:

unicode of Python2 is equivalent to str in Python3, so you can also write:

str(text, 'utf-8')

如果你愿意.

这篇关于如何用python3制作unicode字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 11:44
查看更多