How to read Turkish chars from txt file in Python?
Question:
I am trying to read user credentials from the text file. In the password, there is ‘ü’ character. When I read from txt. It prints ‘l’ character. UTF8 does not work for Turkish characters. How can I read?
def get_username_password():
dosya = open("D:\user.txt","r",encoding="utf8",errors='ignore')
line = dosya.readline()
print(line)
return line.split(",")
eyll,eyll
Answers:
From the screenshot, it looks like you are using Windows. You probably saved the text file as "ANSI" which is a windows term for "whatever encoding I think is appropriate for the location setting". For Turkish, it’s likely Windows-1254.
In python, this encoding is called "cp1254", so the correct code to open the file is:
dosya = open("D:\user.txt","r", encoding="cp1254")
Also you can try this:
dosya = open("D:user.txt","r", encoding=’utf-8′)
I am trying to read user credentials from the text file. In the password, there is ‘ü’ character. When I read from txt. It prints ‘l’ character. UTF8 does not work for Turkish characters. How can I read?
def get_username_password():
dosya = open("D:\user.txt","r",encoding="utf8",errors='ignore')
line = dosya.readline()
print(line)
return line.split(",")
eyll,eyll
From the screenshot, it looks like you are using Windows. You probably saved the text file as "ANSI" which is a windows term for "whatever encoding I think is appropriate for the location setting". For Turkish, it’s likely Windows-1254.
In python, this encoding is called "cp1254", so the correct code to open the file is:
dosya = open("D:\user.txt","r", encoding="cp1254")
Also you can try this:
dosya = open("D:user.txt","r", encoding=’utf-8′)