PyObjC中的Unicode问题

2024-10-04 11:30:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在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)

Tags: namein程序env编码binabusr
3条回答

你将在交互控制台中运行“repr”语句的名称。在

如果将循环的最后一行从“name”改为“print name”,那么输出应该没问题。我已经用终端.app在10.5.7系统上。在

只需写入变量名就会将repr(name)发送到标准输出,repr()对所有unicode值进行编码。在

print试图将u'Jacob \xc5berg'转换为ASCII,但这不起作用。尝试将其写入文件。在

Print Fails on the python wiki。在

That means you're using legacy, limited or misconfigured console. If you're just trying to play with unicode at interactive prompt move to a modern unicode-aware console. Most modern Python distributions come with IDLE where you'll be able to print all unicode characters.

# -*- coding: UTF-8 -*-

如果你的终端设置不符合你的标准,那么它就不会影响到你的终端设置,你应该没事的。在

相关问题 更多 >