错误将字符串编码为python 2.7中的unicode?

想要学习

我想在Python 2.7中打印字符串的unicode版本。它在Python 3中工作正常。但是在python 2.7中,出现以下错误:

x="strings are now utf-8 \u03BCnico\u0394é!"

Python 3:

print('Python', python_version())
print(x)

Python 3.4.1
strings are now utf-8 μnicoΔé!

Python 2.7

>>> x='strings are now utf-8 \u03BCnico\u0394é!'
>>> x.encode('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 38: ordinal not in range(128)

编辑:我尝试了followimg:

>>> x = u'strings are now utf-8 \\u03BCnico\\u0394\xc3\xa9!'
>>> x
u'strings are now utf-8 \\u03BCnico\\u0394\xc3\xa9!'
>>> x.encode("utf-8")
'strings are now utf-8 \\u03BCnico\\u0394\xc3\x83\xc2\xa9!'
>>> x
u'strings are now utf-8 \\u03BCnico\\u0394\xc3\xa9!'

我看不到编码发生

编辑2:

>>> x=u'strings are now utf-8 \u03BCnico\u0394é!'
>>> x.encode("utf-8")
'strings are now utf-8 \xce\xbcnico\xce\x94\xc3\xa9!'
>>> b=x.encode("utf-8")
>>> b
'strings are now utf-8 \xce\xbcnico\xce\x94\xc3\xa9!'
>>> 
琳西蒙

在Python 2.x中,您需要使用unicode文字:

x=u"strings are now utf-8 \u03BCnico\u0394é!"

encode否则,该方法将不知道字符串的编码方式,并假定它是ASCII。然后,它尝试将ASCII转换为UTF-8,并在遇到ASCII字符集以外的字符时失败。

还请注意,Python 3.3及更高版本支持此表示法。在这种情况下,这基本上是一个禁忌,因为所有字符串都假定为unicode,但允许开发人员编写与2.x和3.3+兼容的代码。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

iOS将字符串编码为unicode

来自分类Dev

在SSIS 2012中将Unicode字符串覆盖为非Unicode字符串

来自分类Dev

错误将字符串中的字符转换为大写

来自分类Dev

将Unicode转换为Python 2中的字符串

来自分类Dev

Python 2和3中的字符串均以字节为单位

来自分类Dev

在Python 3中对字符串进行编码而无需unicode \ xc2替代

来自分类Dev

Python-unicode字符串中的ASCII编码字符串;如何删除“ u”?

来自分类Dev

Python中的行为Unicode字符串

来自分类Dev

Python 2.x字符串:Unicode与字节

来自分类Dev

在Python 2中从字符串中剥离时间(格式为hh:mm)

来自分类Dev

python3 pySerial TypeError:不支持Unicode字符串,请编码为字节:

来自分类Dev

如何在Windows下的Python 2 Shell中打印unicode字符串?

来自分类Dev

错误将变量设置为char数组中的特定字符/字符串为char []数组为string []数组为字符串

来自分类Dev

SyntaxError:Unicode字符串中的编码声明

来自分类Dev

如何在Python中区分正确的和错误的unicode编码的字符串?

来自分类Dev

如何在Python中优化2D字符串数组的序数编码?

来自分类Dev

在Python 3中将整数的字符串表示形式编码为base64

来自分类Dev

从delphi 7到xe的字符串编码错误

来自分类Dev

如何在python中将unicode字符串编码为utf-8?

来自分类Dev

C#错误将jQuery中的字符串从jquery传递到WebAPI项目方法

来自分类Dev

Python使用unicode编码的字符串

来自分类Dev

在Python中简化“如果在字符串中为var1或在字符串中为var2:”

来自分类Dev

打开字符串Unicode编码时出现错误

来自分类Dev

将Unicode转换为Python 2中的字符串

来自分类Dev

Python如何解决字符串中的Unicode错误

来自分类Dev

在Excel或OfficeCalc中获取字符串的Unicode(USC-2)值

来自分类Dev

Rapidjson 解析错误:字符串中的编码无效 (207)

来自分类Dev

类型错误:execv() arg 2 必须只包含字符串(子进程和 unicode)

来自分类Dev

在 Python 中确定字符串的编码

Related 相关文章

  1. 1

    iOS将字符串编码为unicode

  2. 2

    在SSIS 2012中将Unicode字符串覆盖为非Unicode字符串

  3. 3

    错误将字符串中的字符转换为大写

  4. 4

    将Unicode转换为Python 2中的字符串

  5. 5

    Python 2和3中的字符串均以字节为单位

  6. 6

    在Python 3中对字符串进行编码而无需unicode \ xc2替代

  7. 7

    Python-unicode字符串中的ASCII编码字符串;如何删除“ u”?

  8. 8

    Python中的行为Unicode字符串

  9. 9

    Python 2.x字符串:Unicode与字节

  10. 10

    在Python 2中从字符串中剥离时间(格式为hh:mm)

  11. 11

    python3 pySerial TypeError:不支持Unicode字符串,请编码为字节:

  12. 12

    如何在Windows下的Python 2 Shell中打印unicode字符串?

  13. 13

    错误将变量设置为char数组中的特定字符/字符串为char []数组为string []数组为字符串

  14. 14

    SyntaxError:Unicode字符串中的编码声明

  15. 15

    如何在Python中区分正确的和错误的unicode编码的字符串?

  16. 16

    如何在Python中优化2D字符串数组的序数编码?

  17. 17

    在Python 3中将整数的字符串表示形式编码为base64

  18. 18

    从delphi 7到xe的字符串编码错误

  19. 19

    如何在python中将unicode字符串编码为utf-8?

  20. 20

    C#错误将jQuery中的字符串从jquery传递到WebAPI项目方法

  21. 21

    Python使用unicode编码的字符串

  22. 22

    在Python中简化“如果在字符串中为var1或在字符串中为var2:”

  23. 23

    打开字符串Unicode编码时出现错误

  24. 24

    将Unicode转换为Python 2中的字符串

  25. 25

    Python如何解决字符串中的Unicode错误

  26. 26

    在Excel或OfficeCalc中获取字符串的Unicode(USC-2)值

  27. 27

    Rapidjson 解析错误:字符串中的编码无效 (207)

  28. 28

    类型错误:execv() arg 2 必须只包含字符串(子进程和 unicode)

  29. 29

    在 Python 中确定字符串的编码

热门标签

归档