我正在尝试在macosx上找出PyObjC,我已经编写了一个简单的程序来打印出通讯簿中的姓名。但是,我在输出的编码方面遇到了一些问题。在
#! /usr/bin/env python
# -*- coding: UTF-8 -*-
from AddressBook import *
ab = ABAddressBook.sharedAddressBook()
people = ab.people()
for person in people:
name = person.valueForProperty_("First") + ' ' + person.valueForProperty_("Last")
name
运行此程序时,输出如下所示:
^{pr2}$有人能解释一下为什么字符串是unicode的,但内容看起来是那样的吗?在
我还注意到,当我试图打印姓名时,我得到了错误
UnicodeEncodeError: 'ascii' codec can't encode character u'\xc5' in position 6: ordinal not in range(128)
你将在交互控制台中运行“repr”语句的名称。在
如果将循环的最后一行从“name”改为“print name”,那么输出应该没问题。我已经用终端.app在10.5.7系统上。在
只需写入变量名就会将
repr(name)
发送到标准输出,repr()
对所有unicode值进行编码。在print
试图将u'Jacob \xc5berg'
转换为ASCII,但这不起作用。尝试将其写入文件。在见Print Fails on the python wiki。在
如果你的终端设置不符合你的标准,那么它就不会影响到你的终端设置,你应该没事的。在
相关问题 更多 >
编程相关推荐