On 26/03/16 10:08, Dimitar Ivanov wrote: > First time using the mailing list to give a suggestion, apologies in > advance if it's not appropriate :)
Join the gang, any suggestions are appropriate since even when they aren't completely correct they offer learning opportunities. So all contributions are welcome. > Considering you're looking for specific string, I'd recommend maybe looking > into the Regular Expressions. You could use something like: The regex approach is good if the pattern is not a simple string. And it also has the advantage that you can findall() in one operation. The downside is that regex are notoriously hard to get right for anything other than trivial. > target = open(filename, 'rU') You should probably use the 'with' style with open(filename, 'rU') as target: # code here > # 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) You need a slash for the second tag: match = re.findall(r'<uicontrol>.*?</uicontrol>', text) > 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') You should either close() the file after writing or use the 'with' style (which closes automatically). Otherwise if your program terminated suddenly your data might not make it from the buffer to the disk. The 'with' style is preferred. with open('Matches', 'w') as new: for m in matches: new.write(m + '\n') But your overall point that a regex can be used is correct, especially if the pattern were not consistent (for example if the tag sometimes had spaces or different cased letters). -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor