Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

download_url should make use of the encoding in the Content-Type header #2

Open
fawkesley opened this issue Nov 14, 2013 · 1 comment

Comments

@fawkesley
Copy link
Contributor

At the moment we're returning a file object from response.content which loses any information we had about the file's unicode encoding:

Content-Type: text/html; charset=UTF-8

We can cunningly wrap the returned file handle using the codecs module:

from cStringIO import StringIO
>>> f = StringIO(a.encode('utf-8'))
>>> f.read()
'Marat\xc3\xb3n'
>>> f.seek(0)
>>> g = codecs.getreader('utf-8')(f)
>>> print g.read()
Maratón
@scraperdragon
Copy link
Contributor

The critical bit of code is:

g = codecs.getreader('utf-8')(f)
g.read()

f is a file handle containing UTF-8 bytes; but g.read() returns correct unicode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants