Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to decode ISO8859-1 in Python 3?

9 views
Skip to first unread message

Chris Green

unread,
Nov 28, 2021, 11:18:20 AM11/28/21
to
This sounds as if it should be trivial but searching only seems to
produce ways ofd doing it in Python 2.

I have some text files which are ISO8859-1 encoded and I want to output
them to screen using Python.

--
Chris Green
·

Chris Green

unread,
Nov 28, 2021, 12:03:22 PM11/28/21
to
Stefan Ram <r...@zedat.fu-berlin.de> wrote:
> Chris Green <c...@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
·
0 new messages