0

I want to read a line from a file and make operation on specific char on that line. I read line as in link on stackoverflow

But there is a problem for me because I want to reach every single char in that file. My code is:

with open(r'C:\Users\BerkayS\Desktop\testfile.txt') as inputFile:
    content = inputFile.read().splitlines()
kelime = content[0:1]

here kelime is a list as expected. But it is a line in file actually, so I want to reach every char in that line, when I write

harf = kelime[2:5]

it returns empty because kelime's length is 1 as a whole string. How can I split all chars and spaces in kelime into a new list that I can manupulate all of them?

Community
  • 1
  • 1
abidinberkay
  • 1,857
  • 4
  • 36
  • 68

2 Answers2

2

kelime is a one-element list where the sole element is a str containing the first line. But you're manipulating it as if you expect it to be a str (or a list of the individual characters).

If you want a str, use:

kelime = content[0]  # As opposed to content[0:1]

to index, not slice. If you want a list of the characters, do:

kelime = list(content[0])

to make a mutable list of the first line's characters.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
1

each kelime it's a list. you should do:

harf = kelime[0][2:5]
elabard
  • 455
  • 1
  • 5
  • 16