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:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |