[Tutor] UnicodeDecodeError
Alan Gauld
alan.gauld at yahoo.co.uk
Mon Mar 16 11:23:10 EDT 2020
On 16/03/2020 14:11, thehouse.be--- via Tutor wrote:
> p.write_text('- ' + (cel))
> File "/Users/travis/build/mu-editor/mu_portable_python_macos/python/lib/python3.6/pathlib.py", line 1214, in write_text
> UnicodeEncodeError: 'ascii' codec can't encode character '\xeb' in position 144: ordinal not in range(128)
>
>
> So probably I have also to put ‘utf-8’ somewhere in the p.write_text code.
The help says:
>>> help(p.write_text)
Help on method write_text in module pathlib:
write_text(data, encoding=None, errors=None) method of pathlib.PosixPath
instance
Open the file in text mode, write to it, and close the file.
Notice the encoding parameter? I think that might be the place...
I've never used pathlib to write to a file before, but I
notice you have write_text inside a loop. From the description
it would seem likely that write_text in a loop will overwrite
the previous text each time round. Might be worth checking?
You may have to use the Path.open() method to open the file
object. (open() also has an encoding parameter)
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list