Unicode issues in PyObjC

I am trying to compute PyObjC on Mac OS X, and I wrote a simple program for printing names in my address book. However, I am having some problems with output encoding.

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

Can someone explain why the strings are in unicode, but the content looks like this?

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

UnicodeEncodeError: 'ascii' codec can't encode character u'\xc5' in position 6: ordinal not in range(128)
+3
source share
4 answers

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

"name" "print name", . Terminal.app 10.5.7.

+1
# -*- coding: UTF-8 -*-

, Python , , , .. .. Mac UTF-8 (Terminal, Preferences, "", "", "" ) Unicode UTF-8 (print name.encode("utf-8")), .

+2

repr(name) , repr() .

print u'Jacob \xc5berg' ASCII, . .

. - python.

This means that you are using an outdated, limited, or improperly configured console. If you're just trying to play with unicode interactively, go to a modern console that supports Unicode. Most modern Python distributions come with IDLE, where you can print all Unicode.

0
source

Convert it to a unicode string with:

print unicode(name)
0
source

Source: https://habr.com/ru/post/1709323/


All Articles