Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

How to append the data from a text file to a list only from a given starting point rather then appending full file using python

I want to append data from text file to a list from a given starting point. The stating point string can be anywhere in the file. i want to append the data from that starting point. I tried by using startswith method:

list1=[] 
TextFile = "txtfile.txt"
# open the file for data processing
with open(TextFile,'rt',encoding="utf8") as IpFile:
    for i,j in enumerate(IpFile):
        if(j.startswith("Starting point")):
            list1.append(str(j).strip()) 
            i+=1      

but it only append the starting point. i want to append the all data from starting point. How to do that ?

question from:https://stackoverflow.com/questions/66045680/how-to-append-the-data-from-a-text-file-to-a-list-only-from-a-given-starting-poi

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use a boolean variable

list1=[] 
TextFile = "txtfile.txt"
doAppend = False
# open the file for data processing
with open(TextFile,'rt',encoding="utf8") as IpFile:
    for i,j in enumerate(IpFile):
        if(j.startswith("Starting point")):
            doAppend = True
        if doAppend:
            list1.append(str(j).strip()) 
            i+=1      


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...