0

I am reading in a file in python:

alist = [line.rstrip() for line in f]
  for line in alist:
    if line[10] == 4:
       hop over 4 lines

Example of the text file:

  • line1
  • line2
  • line3
  • line4
  • line has 4 blabla
  • line want to skip over
  • line want to skip over
  • line want to skip over
  • line want to skip over

When the 10 element in a line is x (some number 1,2,3,4,..) I want to hop over the equal amount of lines. I have searched for an answer for a long time but I can't find anything, help please!

JCole
  • 1
  • 1
  • 1
  • 1
    ok, your question is unclear here. `if line[10] == 4:` will never be true… neither `line[10] == '4'` (i.e. the character `4`) or is it equal to the integer 4. – zmo Jan 18 '17 at 20:32

3 Answers3

2

Try turning alist into an iterable, then you can discard the values you don't want by calling next

alist = [line.rstrip() for line in f]
it = iter(alist)
for line in it:
    print(line)
    if line[10] == 4:
        for i in range(4):
            try: 
                _ = next(it)
            except StopIteration:
                break
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • 1
    I think this is the closest answer we can come up with, BUT `line[10] == 4` still does not makes sense! – zmo Jan 18 '17 at 20:35
  • in which case, why create the listcomp in the first place? why not `for line in f`, simply. – Jean-François Fabre Jan 18 '17 at 20:35
  • Nice, just two points: No need for `_ = ...`, and `f` (the file) is already an iterator, so you could just use that. – tobias_k Jan 18 '17 at 20:35
  • `if line[10] == '4':` would make better sense (or something similar) – Jean-François Fabre Jan 18 '17 at 20:36
  • @tobias_k I'm using `_ = ...` because if python is being used in interactive mode, then the return value is printed to the console if it isn't caught, which kind of ruins the point of the demonstration. You're right that files are iterators, but lists aren't which is what the Op is using at the moment. I agree that his conditional doesn't make any sense – Patrick Haugh Jan 18 '17 at 20:39
1

In order to achieve this, you may iterate over the index of the file using while as:

i = 0   # initialize counter
file_lines = f.readlines()  # It contains the list of all the lines

while i < len(file_lines):
    line = file_lines[i].strip()  # stripped content of `i`th line
    if line[10].isdigit():  # check if 10th character is digit
        match = line[10]
        # Do something

        i += int(match)    # increment the counter by 10
    else:
        i += 1
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
-1

One way you could do this is with a while loop. Here's an example:

i = 0
while i < 20:
  print(i)
  i += 4`

This prints

0
4
8
12
16`

I'm not entirely sure that I understand where and why you're trying to skip lines, so if you can provide that information, I can change the code to be more what you need.

furkle
  • 5,019
  • 1
  • 15
  • 24
  • I don't think this is what the OP is asking. He's not asking how to iterate over values 4 at each iteration. And even if he is, your solution is not best, as you can do `for i in line[0::4]`. – zmo Jan 18 '17 at 20:34