Home > OS >  How to print lines from file if lines are given in list?
How to print lines from file if lines are given in list?

Time:06-09

I'm fairly new to python (using 2.7) so I realize this might be an obvious solution. I'm writing this more to work through the issue on "paper" so to speak.

I have a file containing several different instances of a phrase (phrase="xy Coordinates") and successfully created lists containing the start and end lines of each instance of these coordinates. My question is, now that I have these lists how would I iterate over them to print each line of the coordinate?

This is what I have so far.

def printCart(filename):
    with open(filename) as f:
            CStart=[]
            CEnd=[]
            for i in range(len(slist)):
                    start=slist[i] 5
                    end=start int(num)-1
                    CStart.append(start)
                    CEnd.append(end)
            return CStart,CEnd

CodePudding user response:

In order to iterate through a list and print each element, you can do (suppose your list is called CStart):

for element in CStart:
    print element

CodePudding user response:

It was a pretty easy fix, now I just need to figure out how to give each list a different iterative name. Typing out the problem actually helps!

def finalprint(filename):
    with open(filename) as f:
            lines=f.readlines()
            for line in lines:
                    cart=[]
                    for element in CSlist:
                            if lines[element] in CElist:
                                    break
                            else:
                                    c=lines[element].strip().split()
                                    del c[0:1]
                                    cart.append(c)
    return cart
  • Related