Paul's Programming Notes     Archive     Feed     Github

Writing To File - UnicodeEncodeError: 'ascii' codec can't encode characters

I was getting a lot of errors when I was trying to download a file with the python requests library. The errors looked like the following:

UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 2-6: ordinal not in range(128)

This code ended up fixing it:

import codecs
import requests
import time
# this could be different, but it works for my application, but the content-type definitely matters
fileHeaders = {
'Content-Type': 'application/octet-stream;charset=text/html;charset=UTF-8'
}
url = 'www.yourfile.com/your.zip'
with codecs.open('your.zip', mode='wb', encoding='utf-8') as handle:
zip_file = requests.get(url, headers=fileHeaders, prefetch=False)
for block in zip_file.iter_content(1024):
if block:
handle.write(block)
view raw gistfile1.py hosted with ❤ by GitHub