A
A
Artyom2018-03-20 17:08:16
Python
Artyom, 2018-03-20 17:08:16

Problem with python encoding: files + tkinter, how to fix?

I am writing a small graphical application using python 3 + tkinter. In this piece of code

file = open("testfile.txt", 'r')
t = file.read()
label = Label(root, text=t, font="Arial 14")
label.pack(side="top")
file.close()

text is read from a file and written to a label. But in the window instead of the words "blah blah blah" the text appears, consisting of the characters "P", "±", """, "°". It is noteworthy that English text is displayed normally. How to fix this problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Ruslan., 2018-03-20
@TheProgrammer256

https://docs.python.org/3/library/functions.html#open
Try explicitly specifying the encoding of text a in this file.
Like this:
f = open("myfile.txt", "r", encoding="utf-8")
Or like this:
f = open("myfile.txt", "r", encoding="cp866")

S
Sergey Gornostaev, 2018-03-20
@sergey-gornostaev

Set the encoding parameter of the open function to the encoding of the file being opened.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question