views:

167

answers:

4

I am trying to figure out PyObjC on Mac OS X, and I have written a simple program to print out the names in my Address Book. However, I am having some trouble with the encoding of the output.

#! /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

when I run this program, the output looks something like this:

...snip...
u'Jacob \xc5berg'
u'Fernando Gonzales'
...snip...

Could someone please explain why the strings are in unicode, but the content looks like that?

I have also noticed that when I try to print the name I get the error

UnicodeEncodeError: 'ascii' codec can't encode character u'\xc5' in position 6: ordinal not in range(128)
A: 

Just writing the variable name sends repr(name) to the standard output and repr() encodes all unicode values.

print tries to convert u'Jacob \xc5berg' to ASCII, which doesn't work. Try writing it to a file.

See 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.

Georg
+2  A: 
# -*- coding: UTF-8 -*-

only affects the way Python decodes comments and string literals in your source, not the way standard output is configured, etc, etc. If you set your Mac's Terminal to UTF-8 (Terminal, Preferences, Settings, Advanced, International dropdown) and emit Unicode text to it after encoding it in UTF-8 (print name.encode("utf-8")), you should be fine.

Alex Martelli
A: 

Convert it to a unicode string through:

print unicode(name)
Wim Leers
It already is a unicode string as can be seen by the u'Fernando Gonzales'.
Georg
Maybe, yes, but this *does* fix the problem. I'm not sure why either, I'm not deep enough into Python's string handling.
Wim Leers
+1  A: 

If you run the code in your question in the interactive console the interpreter will print the repr of "name" because of the last statement of the loop.

If you change the last line of the loop from just "name" to "print name" the output should be fine. I've tested this with Terminal.app on a 10.5.7 system.

Ronald Oussoren
Doesn't work for me.
Wim Leers