I am experiencing the weirdest bug of my life.
I am fixing my Hacker News API and this small piece of code is giving me headaches:
from hn import HN
hn = HN()
# print top stories from homepage
for story in hn.get_stories():
print story.title
print story
Story
class a __str__
method as follows:
def __str__(self):
"""
Return string representation of a story
"""
return self.title
(This is a little different from the code in the repo. I had to debug a lot here.)
Anyways, the output is this:
Turn O(n^2) reverse into O(n)
Turn O(n^2) reverse into O(n)
My run-in with unauthorised Litecoin mining on AWS
My run-in with unauthorised Litecoin mining on AWS
Amazon takes away access to purchased Christmas movie during Christmas
Traceback (most recent call last):
File "my_test_bot.py", line 11, in <module>
print story
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 60: ordinal not in range(128)
I have no idea why this is failing. Both the __str__
and the print story
statements print a unicode. Then why is the latter not working?
Also, doing print unicode(story)
works just fine (why??), but unfortunately I cannot use unicode()
since it's not py3 compatible.
title
is encoded as: title.encode('cp850', errors='replace').decode('cp850')
What the hell is happening here? How do I make sure that my API would work for any (meaning most) of the strings it can find and is both py2 and py3 compatible?
I have downloaded the page that is causing this error right now for offline debugging.