Python Forum
Get True of false outside a loop - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Get True of false outside a loop (/thread-12107.html)



Get True of false outside a loop - morgandebray - Aug-09-2018

Hi, how can i get the value of each variable that i'm creating in my loop ? I have this :
for fichier in os.listdir("result/"):
    if not fnmatch.fnmatch(fichier, 'file_0.txt'):

        #récupération de valeur
        with fileinput.input(["result/"+fichier], inplace=1) as file_X :
            # file_X.write('\r\ncoucou')
            print("------Récupération de variable-------")
            for line in file_X:
                balise02R = False
                balise191Absente = True
                line = line.strip('\n')
                if line.startswith('/02/R'):
                    balise02R = True
                    sys.stderr.write("Balise 02 présente /02/R\r\n")
                if line.startswith("/20/"):
                    splitLigne20 = line.split("/")
                    valeur20 = splitLigne20[2]
                if line.startswith("/66/"):
                    splitLigne66 = line.split("/")
                    valeur66 = splitLigne66[2]
                if line.startswith("/191/"):
                    balise191Absente = False
                    sys.stderr.write("Balise 191 présente\r\n")
                print (line.rstrip('\n'))
            # break

        if balise02R == True and balise191Absente == True:
        sys.stderr.write("balise 02 " + str(balise02R) + " / balise 191 " + str(balise191Absente) +"\r\n")
        ...
But variable balise02R hasn't the right value (but it's indead equals to True in trace). I tried break, continue and pass but nothing, any advice ?


RE: Get True of false outside a loop - Larz60+ - Aug-09-2018

perhaps it would be easier understood if you:
  • show several lines of the file verbatim
  • give a list of what you want to match in each line
Then your code will be easier to comprehend.


RE: Get True of false outside a loop - morgandebray - Aug-09-2018

I've got the solution, I changed some variable to true to false and my 2 variables are now declared outside the for :
        with fileinput.input(["result/"+fichier], inplace=1) as file_X :
            balise02R = False
            balise191Presente = False
            for line in file_X:
                line = line.strip('\n')
                if line.startswith('/02/R'):
                    balise02R = True
                if line.startswith("/20/"):
                    splitLigne20 = line.split("/")
                    valeur20 = splitLigne20[2]
                if line.startswith("/66/"):
                    splitLigne66 = line.split("/")
                    valeur66 = splitLigne66[2]
                if line.startswith("/191/"):
                    balise191Presente = True
                print (line.rstrip('\n'))


        if balise02R == True and balise191Presente == True:
            conditionInitial = True