3

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 ?

abidinberkay
  • 1,346
  • 2
  • 21
  • 43

2 Answers2

3

str instances are immutable so str.replace() does not operate in-place but instead returns the result.

But don't do things the hard way.

>>> import unidecode
>>> unidecode.unidecode('İ,Ö,Ü,Ş,Ç,Ğ')
'I,O,U,S,C,G'
Ignacio Vazquez-Abrams
  • 699,552
  • 132
  • 1,235
  • 1,283
0

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.

Community
  • 1
  • 1
abidinberkay
  • 1,346
  • 2
  • 21
  • 43