lundi 10 septembre 2018

Find a string in file with combinaison of loop For and If

I'm beginner on python, and I can't managed to get my loop and if condition working on this case as below.

I'm searching for a server passed in argument (host), If founded it return, the string in column 16th, if not : not founded. In my code it returns not founded in the loop, I just want only one time.

I also tried with one break without luck.

Thanks for our help.

with open('file', 'r') as f:
    for line in f:
        parts = line.split('`')
        if host in line:
          model = parts[16]
          print("You have selected this server model: " + host + " " + model)
        else:
          print("not founded")

Aucun commentaire:

Enregistrer un commentaire