python - 如何在 python 中编码 'Importação de petróleo' 字符串?

标签 python unicode encoding

我想在我的程序中使用“Importação de petróleo”。

我该怎么做,因为所有编码都会给我错误,因为无法编码。

最佳答案

我认为您将字符串 __repr__ 与其 __str__ 混淆了:

>>> s = u"Importação de petróleo"
>>> s
u'Importa\xe7\xe3o de petr\xf3leo'
>>> print s
Importação de petróleo

\xe7 和 friend 没问题;它们只是那些特殊字符的编码表示。你无法避免它们,你不应该需要:)

unicode必读链接: The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)

关于python - 如何在 python 中编码 'Importação de petróleo' 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9644338/

相关文章:

python - 使用 Python 正则表达式匹配不带点的单词

python - 根据范围获取子词典

python - TypeError: &: 'unicode' 和 'unicode' 不支持的操作数类型

ruby - 如何在 Ruby 1.9 中为 unicode 西里尔字符指定正则表达式

python - 查找错误 : unknown encoding: cp0

python - 如何在 conda 中安装 anaconda 中不可用的软件包

python - python 代码在数组中不起作用

python - Unicode 代码点值是否等于每个字符的 UTF-16BE 表示形式?

c - Emacs、xterm、鼠标垫、C、Unicode 和 UTF-8 : Trying to make sense of it all

c# - 借助C#读取txt文件(unicode和utf8)