Reputation: 53
given a line of strings
01 02 03 04 05
06 07 08 09 10
.. .. .. .. ..
I am trying to access the 1st element, "01":
def ElementA (inputFile):
element_A = ""
with open(inputFile, 'r') as inputFile:
for line in inputFile:
for i in line.split():
element_A = i[0:2]
print(element)
For some reason the output is "05". What am I doing wrong?
Upvotes: 2
Views: 34
Reputation: 712
You are doing a little too much. If you want the first element from each line, you can just do this:
def ElementA (inputFile):
element_A = ""
with open(inputFile, 'r') as inputFile:
for line in inputFile:
element_A = line[0:2]
print(element_A)
Upvotes: 1