[Tutor] Removing open bracket, content, close bracket content...
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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Combine Scripts to Work Together...
Hi,I am new to Python, but learning. (Hopefully the layout is readable) I am having trouble getting two scripts to work together. What I want to do is have the combinded script do the following: 1. Read through a directory of xml files, not a single file, but many xml files.2. Read each files contents.3. Pull out the element content (words between open and close tag) * For example: Sam was here. * The script would pull out the content "Sam was here" and print it to a file. My scripts so far:--Script to get content--import xml.etree.ElementTree as ET tree = ET.parse('TEST.xml') root = tree.getroot()for uicontrol in root.iter('uicontrol'): print(uicontrol.text) --Script to read through the files:--import sys import glob import errnopath ='/Users/sastarfa/Documents/Projects/Script-projects/Scripts/Pull-out-terms-between-elements/*.xml' files = glob.glob(path) for name in files: # 'file' is a builtin type, 'name' is a less-ambiguousvariable name. try: with open(name) as f: # Noneed to specify 'r': this is the default. sys.stdout.write(f.read()) This is the area I get stuck. I can understand, write a script to run on one a single file you input into the script, but getting a script to run on a directory I am not understanding how to do this. Thanks in advance for any and all help.Really appreciate the help. Sam ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Can This Script Be Modified to Read Many Files?..
Hi,I am very new to Python, but having fun learning. I need to have a script read all of the XML files contents that are in a directory, pull out the contents of an element, in my case , and list them in an output file. I have this script that does exactly what I need. But in my beginning Python class we didn't go over reading all files, only one file at a time. Can the below script be modified to scan/read all of the XML files in a directory instead of just the file typed into the script? If so, how do I do this? Thanks for all the help.Sam import xml.etree.ElementTree as ETtree = ET.parse('TEST.xml') <-- Want to read all the files in directory, not type in the filename.root = tree.getroot() for uicontrol in root.iter('uicontrol'): print(uicontrol.text) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Removing Content from Lines....
Hello,I am hoping you experts can help out a newbie.I have written some python code to gather a bunch of sentences from many files. These sentences contain the content: blah blah blah blah 1-up printingblah blah blah blah blah blah blah blah blah blah Presetblah blah blah blah blah blah blah Preset blah blah blah What I want to do is remove the words before the and after the . How do I do that in Python? Thanks for any and all help.Sam ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Removing Content from Lines....
Thanks to all who responded. I will try the suggestions and see if I can get the rest of my script to work.Thanks again for the tips. Will also read up on "len" and "index". Cool stuff! On Thursday, March 24, 2016 5:57 PM, Sam Starfas via Tutor wrote: Hello,I am hoping you experts can help out a newbie.I have written some python code to gather a bunch of sentences from many files. These sentences contain the content: blah blah blah blah 1-up printingblah blah blah blah blah blah blah blah blah blah Presetblah blah blah blah blah blah blah Preset blah blah blah What I want to do is remove the words before the and after the . How do I do that in Python? Thanks for any and all help.Sam ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor