Python Forum
Get True of false outside a loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get True of false outside a loop
#1
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 ?
Reply
#2
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.
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  While True loop help Nickd12 2 3,174 Oct-17-2020, 08:12 AM
Last Post: Nickd12
  Is using while True loop good? escape_freedom13 5 6,761 Jul-03-2020, 08:27 PM
Last Post: escape_freedom13
  While loop = False NectDz 1 2,737 Jun-03-2020, 04:35 PM
Last Post: GOTO10
  difference between «1 in [2] == False» and «(1 in [2]) == False» fbaldit 2 3,437 Apr-20-2020, 05:39 PM
Last Post: fbaldit
  While loop doesn't end when False Kanashi 2 4,171 Nov-21-2019, 02:38 AM
Last Post: Kanashi
  Do break operators turn while loop conditions from True to False? Drone4four 5 5,206 Oct-24-2019, 07:11 PM
Last Post: newbieAuggie2019
  How to use while true/while loop on python christing 4 4,718 Oct-08-2019, 08:02 AM
Last Post: perfringo
  Returning True or False vs. True or None trevorkavanaugh 6 19,768 Apr-04-2019, 08:42 AM
Last Post: DeaD_EyE
  file.write stops while true loop from executing in python3 boonr 3 4,683 Mar-25-2019, 12:50 PM
Last Post: ichabod801
  How to make loop go back to the beginning if test evaluaes to True. FWendeburg 1 3,869 Feb-13-2019, 01:26 AM
Last Post: stullis

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.