Search code examples
pythonnode-red

"pythonshell in" node failing because there's an apostrophe in the output


I'm using a Python script which returns the currently playing/on air DJs on the BBC at the moment. Thought it was working error free but now I find it fails if there's a (weird) apostrophe "’" in the output. Can I fix this somehow?

exit code: 1, Traceback (most recent call last):
  File "script2.py", line 17, in <module>
    print g_data[5].text
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 7: ordinal not in range(128)

this is the output

Clara Amfo
Radio 1 Dance
Ace
Ken Bruce
Essential Classics
Hip-hop’s Laughing Stock
Desert Island Discs Revisited
The Emma Barnett Show
Cricket
Mary Anne Hobbs

Solution

  • The issue here is that the that you have in your string is not ascii. The ascii equivalent is ', note the difference.

    Getting unicode errors when printing is common in Python, and what you need to do is encode the string before you print it.

    eg.

    print g_data[5].text.encode('utf-8')
    

    More information can be found in the Python documentation - https://docs.python.org/2.7/howto/unicode.html#the-unicode-type