Read in every line that starts with a certain character from a file

后端 未结 3 2042
一个人的身影
一个人的身影 2021-01-19 12:19

I am trying to read in every line in a file that starts with an \'X:\'. I don\'t want to read the \'X:\' itself just the rest of the line that follows.

with o         


        
3条回答
  •  野性不改
    2021-01-19 12:37

    for line in f:
            search = line.split
            if search[0] = "X":
                storagearray.extend(search)
    

    That should give you an array of all the lines you want, but they'll be split into separate words. Also, you'll need to have defined storagearray before we call it in the above block of code. It's an inelegant solution, as I'm a learner myself, but it should do the job!

    edit: If you want to output the lines, simply use python's inbuilt print function:

    str(storagearray)    
    print storagearray    
    

提交回复
热议问题