Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ol> <li>Just extract all the interesting symbols from a line. </li> <li>Push the opening symbols onto stack and pop from the stack whenever you get a closing symbol. </li> <li>If the stack is clean, the symbols are balanced. If the stack underflows or doesn't get fully unwound you have unbalanced line. </li> </ol> <p>Sample code for checking a line follows - I've inserted a stray bracket into the first line.</p> <pre><code>d = """SL ID=0X14429A0B TY=STANDARD OWN=0X429A(03 EXT=22 SLTK=0X1C429A0B MP=0X684003F0 SUB=0X24400007 RT ID=0X18429A19 TY=CALONSC OWN=0X14429A0B EXLP=0X14429A0C CMDS=(N:0X8429A04,C:0X14429A0B) SGCC=2 REL=1 DESC="AURANT YD-INDSTRY LD" ATIS=T RT ID=0X18429A1A TY=CALONSC OWN=0X14429A0B EXLP=0X14429A08 CMDS=(R:0X8429A04,N:0X8429A05,C:0X14429A0B) SGCC=2 REL=2 DESC="AURANT YD TO TRK.1" ATIS=T RT ID=0X18429A1B TY=CALONSC OWN=0X14429A0B EXLP=0X14429A0A CMDS=(R:0X8429A04,R:0X8429A05,C:0X14429A0B) SGCC=2 REL=3 DESC="AURANT YD TO TRK.2" ATIS=T SL ID=0X14429A0C TY=STANDARD OWN=0X429A03 EXT=24 SLTK=0X1C429A0B MP=0X684003F1 SUB=0X24400007 RT ID=0X18429A1C TY=CALONSC OWN=0X14429A0C EXLP=0X14429A0B CMDS=(N:0X8429A04,C:0X14429A0C) SGCC=2 REL=1 DESC="AURANT YD-INDSTRY LD" ATIS=T TK ID=0X1C429A08 TY=BLKTK OWN=0X429A03 EXT=12 LRMP=0X6C40BDAF LEN=5837 FSPD=60 PSPD=65 QUAL=TRK.1 MAXGE=0 MAXGW=0 JAL=4 ALT=12 SUB=0X24400007 RULES=(CTC:B:UP:0X24400007:485.7305:486.8359:T) LLON=-118.1766772 RLON=-118.1620059 LLAT=34.06838375 RLAT=34.07811764 LELE=416.6983 RELE=425.0596 ULAD=NO URAD=NO PT ID=0X20429A0F TY=STANDARD OWN=0X1C429A08 LTK=0X1C40006C RTK=0X1C429A0C REL=1 LEN=1 LQUAL="TRK.1" RQUAL="TRK.1" PTK OWN=0X1C429A08 PID=0X1C429A13""" def unbalanced(line): close_symbols = {'"' : '"', '(': ")", '[': ']', "'" : "'"} syms = [x for x in line if x in '\'"[]()'] stack = [] for s in syms: try: if len(stack) &gt; 0 and s == close_symbols[stack[-1]]: stack.pop() else: stack.append(s) except: # catches stack underflow or closing symbol lookup return True return len(stack) != 0 print unbalanced("hello 'there' () []") print unbalanced("hello 'there\"' () []") print unbalanced("][") lines = d.splitlines() # in your case you can do open("file.txt").readlines() print [line for line in lines if unbalanced(line)] </code></pre> <p>For large files, you don't want to read all the files into memory so use fragment like this instead:</p> <pre><code>with open("file.txt") as infile: for line in infile: if unbalanced(line): print line </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload