[Tutor] Removing Content from Lines....
Dimitar Ivanov
dimitarxivanov at gmail.com
Sat Mar 26 06:08:02 EDT 2016
Hello everyone,
First time using the mailing list to give a suggestion, apologies in
advance if it's not appropriate :)
Considering you're looking for specific string, I'd recommend maybe looking
into the Regular Expressions. You could use something like:
#At the top of the file
import re
#Within your function
target = open(filename, 'rU')
# This will read the file as a whole string, careful, it loads the file
into memory so if it's a big file, it might cause troubles
text = target.read()
# The regular expression should match every single tag and its content
within the file, using '.*?' will match each character without being greedy.
match = re.findall(r'<uicontrol>.*?<uicontrol>', text)
Afterwards, you can use something like:
for i in match:
print i
This should print out all the matches to Stdout so you can verify the RegEx
behaves as expected. To write the matches in a new file, you could do:
new = open('Matches', 'w') ## This will create a new file called
'Matches' for writing
for i in match:
new.write(i + '\n') ## Each match of the RegEx will be written on a new
line in the Matches file.
At the end, my function looks like this:
def textfind(filename):
target = open(filename, 'rU')
text = target.read()
match = re.findall(r'<uicontrol>.*?</uicontrol>', text)
new = open('Matches', 'w')
for i in match:
new.write(i + '\n')
Regards,
Dimitar
--
Thanks,
Dimitar
*Twitter:* @winterchillz
*Facebook: */dimitarxivanov
More information about the Tutor
mailing list