mercredi 30 janvier 2019

How to open a file in python, read the comments ("#"), find a word after the comments and select the word after it?

I have a function that loops through a file that Looks like this:

"#" XDI/1.0 XDAC/1.4 Athena/0.9.25

"#" Column.4: pre_edge

Content

That is to say that after the "#" there is a comment. My function aims to read each line and if it starts with a specific word, select what is after the ":"

For example if I had These two lines. I would like to read through them and if the line starts with "#" and contains the word "Column.4" the word "pre_edge" should be stored.

An example of my current approach follows:

with open(file, "r") as f:
        for line in f:
            if line.startswith ('#'):
                word = line.split(" Column.4:")[1]
            else:
                print("n")

I think my Trouble is specifically after finding a line that starts with "#" how can I parse/search through it? and save its Content if it contains the desidered word.

Aucun commentaire:

Enregistrer un commentaire