You can let Python guess the encoding of a file.
def encoding_of( name ):
path = pathlib.Path( name )
for encoding in( "utf_8", "cp1252", "latin_1" ):
try:
with path.open( encoding=encoding, errors="strict" )as file:
On 25 Oct 2022, at 11:16, Stefan Ram <[email protected]> wrote:
[email protected] (Stefan Ram) writes:
You can let Python guess the encoding of a file.
def encoding_of( name ):
path = pathlib.Path( name )
for encoding in( "utf_8", "cp1252", "latin_1" ):
try:
with path.open( encoding=encoding, errors="strict" )as file:
I also read a book which claimed that the tkinter.Text
widget would accept bytes and guess whether these are
encoded in UTF-8 or "ISO 8859-1" and decode them
accordingly. However, today I found that here it does
accept bytes but it always guesses "ISO 8859-1".
main.py
import tkinter
text = tkinter.Text()
text.insert( tkinter.END, "AÄäÖöÜüß".encode( encoding='ISO 8859-1' )) text.insert( tkinter.END, "AÄäÖöÜüß".encode( encoding='UTF-8' )) text.pack()
print( text.get( "1.0", "end" ))
output
AÄäÖöÜüßAÃäÃöÃüÃ
--
https://mail.python.org/mailman/listinfo/python-list
On 25 Oct 2022, at 11:16, Stefan Ram <[email protected]> wrote:
[email protected] (Stefan Ram) writes:
You can let Python guess the encoding of a file.
def encoding_of( name ):
path = pathlib.Path( name )
for encoding in( "utf_8", "cp1252", "latin_1" ):
try:
with path.open( encoding=encoding, errors="strict" )as file:
I also read a book which claimed that the tkinter.Text
widget would accept bytes and guess whether these are
encoded in UTF-8 or "ISO 8859-1" and decode them
accordingly. However, today I found that here it does
accept bytes but it always guesses "ISO 8859-1".
The best you can do is assume that if the text cannot decode as utf-8 it may be 8859-1.
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 715 |
| Nodes: | 16 (2 / 14) |
| Uptime: | 153:58:29 |
| Calls: | 12,091 |
| Calls today: | 4 |
| Files: | 15,000 |
| Messages: | 6,517,674 |