1

im triying to iterate a file and start the loop in the second item of the list.

file = open(filename, 'r', encoding='iso-8859-1')
for line in file:
   # here i want to start the for in the second row
    line = line.strip('\n')

Any tips?

I tried doing:

start_row = 2
end_row = 10
file = open(filename, 'r', encoding='iso-8859-1')
for line in file[start_row:end_row]:
   # here i want to start the for in the second row
    line = line.strip('\n')

But i cant index a IOTextWrapper.

0

1 Answer 1

1

Just skip the 1st record with next call and use context manager to cover file's open/close phases:

with open(filename, 'r', encoding='iso-8859-1') as f:
   next(f)
   for line in f:
       line = line.strip('\n')
Sign up to request clarification or add additional context in comments.

3 Comments

Why do you add next(f) before the for loop? I want to skip the first line. with open(filename, 'r', encoding='iso-8859-1') as f: for line in f: if start_row > line: next(line) line = line.strip('\n')
@AlexRibeiro, nothing wrong there, cause f is iterator
Hey. Finally what i did was just use the continue like this: start_row = 2 end_row = 10 count = 0 file = open(filename, 'r', encoding='iso-8859-1') for line in file: count =count+1 if count < start_row or count > end_row: continue line = line.strip('\n')

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.