ayse
ayse

Reputation: 23

how to use non-english literal without getting any unicode error?

i have so many text label in my gui app that are not english. so I am getting unicode error.

leaveBtn = Button(top_frame_label, text= u"G�rev Y�k�n� Ay�r".decode(errors='replace') , width = 15)
                                                                 ^
SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xf6 in position 1: invalid start byte

in the first code I wrote I didn't get any error until I use an array that has so many strings that have non-english literals. because it is an array before using them I encoded them and then decoded them when I use them. (I added the code) I haven't had any other unicode issues.

from tkinter import *

root = Tk()
root.state('zoomed')

teleTitle_f_tab2 = ["Takım no: ","Paket no: ","Zaman: ",
        "G.Y. Basıncı: ","T. Basıncı: ",
        "Nem: ",
        "G.Y. Yüksekliği: ","T. Yüksekliği: ",
        "İrtifa Farkı: ","İniş Hızı: ","Sıcaklık: ","Pil Gerilimi: ",
        "G.Y. Latitude: ","G.Y. Longitude: ","G.Y. Altitude: ",
        "T. Latitude: ","T. Longitude: ","T. Altitude: ",
        "Durum: ",
        "Pitch: ","Roll: ","Yaw: ",
        "Dönüş Sayısı: ","Video Aktarım Bilgisi: ", " "]
    
for i in range(0,25):
    teleTitle_f_tab2[i] = teleTitle_f_tab2[i].encode('UTF-8')
data = 0
for col in range(5):
    for rw in range(5):
        textData = Label(root, text= teleTitle_f_tab2[data].decode(), anchor = "w",width= 28)
        textData.grid(row=rw, column=col, padx=5, pady=10)
        data = data +1 
root.mainloop()

then I moved my code to another .py file because they were so messy. and now I'm getting unicode errors. First, I wrote before every text u"" but it couldn't help. then i tried .decode(errors='replace') in my first text that has non-english literal as you can see above. but it couldn't help too. and also I tried u"ascii_text".encode('UTF-8').decode() but it doesn't work too. every time same error. what can I do now? I have so much text like this in buttons, frame titles, or labels.

edit: the code ran in python idle properly but doesn't work in visual studio.

Upvotes: 1

Views: 113

Answers (2)

ayse
ayse

Reputation: 23

I found a solution while searching. I hope it would help you if you have the same problem. the solution is simply to save your file as an encoding: Unicode (UTF-8 Without signature).

UTF-8 without BOM

Upvotes: 0

Carl_M
Carl_M

Reputation: 948

I ran the following test

teleTitle_f_tab2 = ["Takım no: ", "Paket no: ", "Zaman: ",
                    "G.Y. Basıncı: ", "T. Basıncı: ",
                    "Nem: ",
                    "G.Y. Yüksekliği: ", "T. Yüksekliği: ",
                    "İrtifa Farkı: ", "İniş Hızı: ", "Sıcaklık: ",
                    "Pil Gerilimi: ",
                    "G.Y. Latitude: ", "G.Y. Longitude: ", "G.Y. Altitude: ",
                    "T. Latitude: ", "T. Longitude: ", "T. Altitude: ",
                    "Durum: ",
                    "Pitch: ", "Roll: ", "Yaw: ",
                    "Dönüş Sayısı: ", "Video Aktarım Bilgisi: ", " "]

for item in teleTitle_f_tab2:
    print(item)

And got the following output. It does not seem the encode is necessary.

Takım no: 
Paket no: 
Zaman: 
G.Y. Basıncı: 
T. Basıncı: 
Nem: 
G.Y. Yüksekliği: 
T. Yüksekliği: 
İrtifa Farkı: 
İniş Hızı: 
Sıcaklık: 
Pil Gerilimi: 
G.Y. Latitude: 
G.Y. Longitude: 
G.Y. Altitude: 
T. Latitude: 
T. Longitude: 
T. Altitude: 
Durum: 
Pitch: 
Roll: 
Yaw: 
Dönüş Sayısı: 
Video Aktarım Bilgisi:

Upvotes: 1

Related Questions