Search code examples
stringpython-3.xutf-8charnon-english

Python3 Converting Non-English Chars to English Chars


I have a text file, I read file and after some operation I put these lines into another file. But input file has some Turkish chars such as "İ,Ö,Ü,Ş,Ç,Ğ". I want these chars to be converted to English chars because when I open the files in UTF-8 encoding, these chars are not shown. My code is below:

for i in range (len(singleLine)):
        if singleLine[i] == "İ":
            singleLine.replace(singleLine[i:i+1],"I")
        if singleLine[i] == "Ü":
            singleLine.replace(singleLine[i:i + 1], "U")
        if singleLine[i] == "Ö":
            singleLine.replace(singleLine[i:i + 1], "O")
        if singleLine[i] == "Ç":
            singleLine.replace(singleLine[i:i + 1], "C")
        if singleLine[i] == "Ş":
            singleLine.replace(singleLine[i:i + 1], "S")
        if singleLine[i] == "Ğ":
            singleLine.replace(singleLine[i:i + 1], "G")
    return singleLine

But the code does not recognize these Turkish chars in Input file and putting them into outputfile without any operation.

What is the way to recognize these chars? Is there any special way for ASCII code based search or something like this ?


Solution

  • as in the comment: answer for switch case

    I used that method as:

    choices = {"İ":"I", "ş" : "s"...}
            singleLine = singleLine.replace(singleLine[i:i+1],choices.get(singleLine[i],singleLine[i]))
    

    and it is solved.