我编写了这个 Python 脚本来列出 Launchpad 中团队成员的所有 display_name:
from launchpadlib.launchpad import Launchpad
cachedir = "/home/username/.launchpadlib/cache/"
launchpad = Launchpad.login_anonymously('just testing', 'production', cachedir)
team = launchpad.people['']
len(team.members)
for person in team.members:
print person.display_name
exit()
但是,当我执行脚本并将其导入到输出文件中时,如果找到非 ASCII 名称则会失败:
$ python get_list.py > /tmp/file
Traceback (most recent call last):
File "get_list.py", line 7, in <module>
print person.display_name
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 8: ordinal not in range(128)
有任何想法吗?
答案1
使用
print person.display_name.encode('utf-8')
告诉 Python 对 unicode 字符串使用什么字符编码。