Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've written this python script to list all the display_name's from a team members in Launchpad:

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()

But when I execute the script piping it into an output file, it fails when finding a non-ascii name:

    $ 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)

Any ideas?

share|improve this question
up vote 2 down vote accepted

Use

  print person.display_name.encode('utf-8')

to tell Python what character encoding to use for unicode strings.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.