mardi 26 novembre 2019

Python - if line contans x and doesnt end with an ip address

I'm quite new to python and would appreciate some assistance/suggestions.

I have a text file which I need to add a new line to if some conditions are/aren't met. I need to look over each line for lines that contain "rightid=" followed by either an IP address or a name.

If the line does NOT include an IP address after "rightid=" then add a new line underneath with some text, if it does contain an IP then do nothing.

I'm assuming I should use a regular expression of some sort but I'm drawing a blank on how to structure the code and then how to add a new line underneath

Below is as far as I've got

with open (filename, 'r+') as file:
        data = file.read()
        for line in data:
            if "rightid=" in line:

Any help would be amazing.

Thanks

Aucun commentaire:

Enregistrer un commentaire