[Tutor] Removing open bracket, content, close bracket content...

Sam Starfas starfas_s at yahoo.com
Wed Jan 13 17:25:29 EST 2016


Hi,I am new to Python and trying to create a script that will remove content wrapped in brackets.For example I want to remove from each line the following:[!L] [20][!20+:2]etc....
But when I run my script I only get the last line of the content. It is correct as far as the output I want, but I don't understand why the other lines disappear. 
Here is my data:
[!L]KEXITSETUP=Exit Setup[20]KEXITPSSETUP=Exit PS Setup[20]KEXITCOLSETUP=Exit Color SetupKSERVERSETUP=Server Setup[!L]KPRINTERSETUP=Printer Setup[!L]KNETWORKSETUP=Network Setup[!L]KJOBLOGSETUP=Job Log Setup[!L]KLANGUAGESETUP=Language Setup[!L]KCHANGEPASSWRD=Change Password[!L]KCLRSRVR=Clear Server[!L]KFACTORYDFLT=Factory DefaultsSETUPB1=Setup[20+:2]KPSERROR=Print to            PS Error[20+:2]PSERROR=Print to            PS Error[20+:2]EFPSError=Print to            PS Error

----------------------------------------------------------------Here is the script I have so far (remember I am new):
import re
with open("lcd_eng.dct") as f:    with open("out.txt", "w") as f1:        for line in f:            if f == 0 or not line.lstrip().startswith('#'):                f1.write(line)
# Converts the = sign to a , comma for generation of the .csv filef = open("out.txt",'r')filedata = f.read()f.close()newdata = filedata.replace("=",",")f = open("out2.txt",'w')f.write(newdata)f.close()
# Should strip out anything [...] f1 = open("out2.txt",'r')newdata = f1.read()f1.close()newdata = re.sub(r'\[[^>]*\]', '', newdata)f1 = open("end.txt",'w')f1.write(newdata)f1.close()

The output I get is only the last line of the input file. Not sure why. I know the issue is in the search and replace, but I am not sure where. 
Thanks for any and all help.Very much appreciated.Sam


More information about the Tutor mailing list