-2

How does this enumerate works? I want a specific starting index but yet the loop goes too far(index out of range)

def endingIndexOfTable(file, index):
    
    r = re.compile('^V.*(.).*(.).*(.).*(-).*(-).*(.).*(.).*(:).*$')
    for i, line in enumerate(file, start= index):
        if list(filter(r.match, line)) or "Sales Tax" in line:
            return i

I want my program to start searching from line index and to return the line where I find the string I am looking for.

quamrana
  • 37,849
  • 12
  • 53
  • 71
chris09trt
  • 91
  • 4
  • Does this answer your question? [Read a file starting from the second line in python](https://stackoverflow.com/questions/46270638/read-a-file-starting-from-the-second-line-in-python) – CrazyChucky Feb 26 '22 at 13:52
  • 1
    Are you confusing `enumerate` with `itertools.islice`? Compare `list(enumerate("abcd", start=2))` with `list(islice("abcde", None, 2))`. – chepner Feb 26 '22 at 14:03
  • 1
    `enumerate` just numbers whatever it sees in `file`; it doesn't skip any elements of it. – chepner Feb 26 '22 at 14:04

2 Answers2

1

I don't think you can start at a specific line of a file. I think you have to skip all the preceding lines first:

def endingIndexOfTable(file, index):
    
    r = re.compile('^V.*(.).*(.).*(.).*(-).*(-).*(.).*(.).*(:).*$')
    for i, line in enumerate(file):
        if i >= index:
            if list(filter(r.match, line)) or "Sales Tax" in line:
                return i

Although, did you mean return line?

Then, the version with islice should be like this:

from itertools import islice

def endingIndexOfTable(file, index):
    
    r = re.compile('^V.*(.).*(.).*(.).*(-).*(-).*(.).*(.).*(:).*$')
    for i, line in islice(enumerate(file), index, None):
        if list(filter(r.match, line)) or "Sales Tax" in line:
            return i

(again assuming that both the regex and the return are correct)

quamrana
  • 37,849
  • 12
  • 53
  • 71
  • 2
    `list(filter(r.match, line))` should always produce `[]`, no? A regex that requires more than one character to match is applied to every character in `line`. – timgeb Feb 26 '22 at 13:55
  • 1
    Sorry, @timgeb: I have no idea how `regex` works. I just copied the code from the OP assuming they knew what they were doing. – quamrana Feb 26 '22 at 13:56
  • `islice` effectively lets you start later. Yes, you still have to *read* those lines, but `islice` silently discards them. – chepner Feb 26 '22 at 14:53
-2

EDIT

I screwed up in the same way as OP. This does not answer the question.


I don't want to deal with your regex, but here's one way to achieve the logic you need for searching from a specific line. It would load the entire file in memory though, and not actually read just the specific line.

poem.txt is just the file I used to test. Contents:

Author of the poem is: Me 
 poem is called: Test
AAFgz
S2zergtrxbhcn
Dzrgxt
Frhgc
Gzxcnhvjzx
xghrfcan a
jvzxhdyrfcv
kh
def read_by_line(file, index):
    for i, line in enumerate(file.readlines(), start=index):
        print(line)
        if "a" in line:  # if condition could have been your regex stuff
            return i

with open('poem.txt', 'r') as file_object:
    print(read_by_line(file_object, 5))
eccentricOrange
  • 876
  • 5
  • 18
  • 2
    I fail to see how it solves any problem the.OP actually has, mainly starting from a specific line number... – Thierry Lathuille Feb 26 '22 at 14:28
  • @ThierryLathuille it works for me. If I pass `index=3` as an argument, the program prints `6`; if I pass `index=0`, it prints `1`. So the loop starts acting only from the specified index/line number. Editing in poems.txt for reference. I'm unable to solve *loading/reading* from a specific line, but it can do the regex *matching/searching* from a specific line—and that's what OP asked for. – eccentricOrange Feb 26 '22 at 14:35
  • 2
    No, it doesn't, only the numbering changes. Your code shows the same wrong assumption about `enumerate` as the OP's code, which is addressed in the comments under the question. – Thierry Lathuille Feb 26 '22 at 14:40
  • @ThierryLathuille Alright. I understood—I'm essentially only offsetting the counter. Thanks for clarifying. Editing in a warning. – eccentricOrange Feb 26 '22 at 14:41
  • @ThierryLathuille I suppose you can edit this according to [the method from one of the other comments](https://stackoverflow.com/a/51276080/10907280) and do something like `enumerate(file.readlines()[index:])` – eccentricOrange Feb 26 '22 at 14:45