I have written a code (please see below) to read a text file and then search in each line a range of GW (in this sample it goes from GW-0 until GW-9, but in reality I will handle bigger ranges of thousands values) together with SNB.
To summarize, I want to count for a given range of GWs how many SNB values are present.
I tried different ways of modifying my regular expressions but still didn’t get the desired output.
Using the code below I should have 8 as an ouput , since 8 GWs do have SNB number and 2GWs don’t.
Can you please help me figure out what is wrong with the regular expressions I am using?
The sample from text file is as follows:
DEV STATE RDM TYPE ADM ABS SNB SNBST LIST GW-0 NEW H'0 321xxxxxx NEW GW-1 BLOC LEN NC H'0 GW-2 NEW H'40 321xxxxxx NEW GW-3 NEW H'0 321xxxxxx NEW GW-4 NEW H'0 321xxxxxx NEW GW-5 NEW H'0 321xxxxxx NEW GW-6 NEW H'0 GW-7 NEW H'0 321xxxxxx NEW GW-8 NEW H'0 321xxxxxx NEW GW-9 NEW H'0 321xxxxxx NEW
And the part of the code is:
import os import re mylines =  idles = nb_500=0 with open ("E:\Data\Projet situatis\Output_st.txt", "rt") as myfile: for myline in myfile: mylines.append(myline.rstrip('\n')) #Search all GWs for RG 0-9 GWUID_500_1=re.search(r'^GW-[0-9] ',myline) SNB=re.search(r'*',myline) if GWUID_500_1 and SNB: nb_500+=1 print (nb_500)