How to decode ISO8859-1 in Python 3?
Chris Green
cl at isbd.net
Sun Nov 28 11:53:57 EST 2021
Stefan Ram <ram at zedat.fu-berlin.de> wrote:
> Chris Green <cl at isbd.net> writes:
> >I have some text files which are ISO8859-1 encoded and I want to output
> >them to screen using Python.
>
> Well, the first attempt would be:
>
> import pathlib
> name = r"C:\example.txt"
> encoding = r"iso8859-1"
> path = pathlib.Path( name )
> with path.open( encoding=encoding, errors="ignore" )as file:
> print( file.read() )
>
> , but if you write to a console which needs a special encoding,
> you might need:
>
> sys.stdout.buffer.write( file.read().encode( 'your screen encoding' ))
>
> (which is not always possible) instead of "print( file.read())",
> and "import sys" at the start of the script.
>
Thanks! It's rather messy isn't it! :-)
I think I'll try converting the files before letting Python loose on
them, iconv should manage that.
--
Chris Green
·
More information about the Python-list
mailing list