[Biopython] Printing non-ASCII characters

Wibowo Arindrarto w.arindrarto at gmail.com
Sat Dec 8 20:53:22 UTC 2012


Hi Melissa,

Does the error you're getting look like this:

UnicodeEncodeError: 'ascii' codec can't encode character u'\u015f' in
position 2: ordinal not in range(128)

If so, it's because there are non-ascii characters in the text you're
trying to print. To try fix that, change the last line to this:

print article['MedlineCitation']['Article']['ArticleTitle'].encode('utf-8')

This forces python to encode the characters using utf-8, which can
handle non-ascii characters. Note that for the characters to be shown
properly (e.g. a with umlaut), your terminal must also support utf-8.
Otherwise, you'll simply get a weird replacement character for all the
non-ascii characters.

Hope this helps :),
Bow

On Sat, Dec 8, 2012 at 9:39 PM, Melissa Curran
<melissacurran530 at gmail.com> wrote:
> Hello,
>
> I'm trying to print out the titles of articles, but often get an error
> about it encountering non-ASCII characters. What do I need to do in order
> to be able to print the article titles? Below is a snippet of code from my
> program, which is where I'm getting the error.
>
>     handle = Entrez.efetch(db="pubmed", id=record, retmode="xml")
>     articles = Entrez.parse(handle)
>     for article in articles:
>         print article['MedlineCitation']['Article']['ArticleTitle']
>
> Thanks much,
>
> Melissa
> _______________________________________________
> Biopython mailing list  -  Biopython at lists.open-bio.org
> http://lists.open-bio.org/mailman/listinfo/biopython



More information about the Biopython mailing list