python - 在 Python 中使用编码方法。任何类似于 map() 的解决方法?

标签 python encoding

我是 Python 的新手,我找不到解决这个特定问题的方法。

对于列表中的每个汉字,我必须一个一个地对每个汉字进行编码。

for character in list:
    print character.encode('utf8')

是否有类似于map(function, characters)的东西,我可以在其中将编码方法应用于列表中的所有字符?

最佳答案

使用 str.join (unicode.join) 将字符连接成一个字符串并对其进行编码。

u''.join(characters).encode('utf8')

>>> characters = [u'世', u'上']

>>> u''.join(characters)
u'\u4e16\u4e0a'
>>> print u''.join(characters)  # unicode
世上

>>> u''.join(characters).encode('utf-8')
'\xe4\xb8\x96\xe4\xb8\x8a'
>>> print u''.join(characters).encode('utf-8')  # str (encoded)
世上

顺便说一句,不要使用 list 作为变量名。它隐藏了内置函数/类型 list .

关于python - 在 Python 中使用编码方法。任何类似于 map() 的解决方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25949272/

相关文章:

python - 用 map 组合函数

python - Python中的Url解码UTF-8

ios - 使用 AudioConverter 进行 AAC 编码并写入 AVAssetWriter

mysql - R RMySQL 查询使日文字符变形

javascript - 使用 Django 将 html5 Canvas 加载到 PIL 图像中

python - 从字符串中提取值

python - 使用计数器的 While 循环

Python:如何从 argparse 获取所有默认值

java - HL7 HAPI - Unicode 字符\E 出现吗?

.net - 从 Firebird 数据库中获取 ASCII 字符串