utf 8 - Writing to file in python gives ascii error -


i'm trying write results web scraping html file. i'm using beautiful soup scrape links , text web pages. when i'm creating file , writing it, following error:

unicodeencodeerror: 'ascii' codec can't encode characters in position 939-940: ordinal not in range(128) 

the line writing file looks this:

file_object.write(file_content) 

and when instead this:

file_object.write(file_content.encode('utf-8')) 

i don't error, can't print special characters, å or ä.

i realize kind of encoding error, can't understand how around it. project in entirety is located here, line 81, since had trouble extracting runnable , logical sub parts.

i'm using mac, had similar problem running same script on pc. using python 2.7

yes use open() codecs module, or, in python 3 normal (built-in) open() this:

f = open(path, "wt", encoding="utf-8") 

but, if don't want change code much, not need special. trick add correct bom (byte order mark) @ beggining of file, editor opens knows utf-8 file, , should treat such.

change should make: file_object.write('\xef\xbb\xbf'+file_content.encode('utf-8')) 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -