python - Can't write to file but can write to text -


i created function convert(), turns pdf html , outputs html string. when :

print(convert()) 

it works, when try write result file:

f.write(convert()) 

i get:

unicodeencodeerror: 'charmap' codec can't encode character '\ufb01' in position 978: character maps <undefined> 

in pycharm project encoder set utf-8, , have

# -*- encoding: utf-8 -*- 

at beginning of file. ideas on why error?

the python version makes difference. here's python 3.6:

python 3.6.2 (v3.6.2:5fd33b5, jul  8 2017, 04:57:36) [msc v.1900 64 bit (amd64)] on win32 type "help", "copyright", "credits" or "license" more information. >>> print('\ufb01') fi >>> open('out.txt','w') f: ...  f.write('\ufb01') ... traceback (most recent call last):   file "<stdin>", line 2, in <module>   file "d:\dev\python36\lib\encodings\cp1252.py", line 19, in encode     return codecs.charmap_encode(input,self.errors,encoding_table)[0] unicodeencodeerror: 'charmap' codec can't encode character '\ufb01' in position 0: character maps <undefined> 

the reason in case python 3.6 on windows writes console unicode apis, works nicely. opening file default encoding uses code page 1252 on system, doesn't support unicode character written. use encoding supports unicode characters:

>>> open('out.txt','w',encoding='utf8') f: ...  f.write('\ufb01') ... 1 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -