Replace unknow string varible in file.
vlastimil.brom at gmail.com
Tue Feb 10 22:22:37 CET 2009
2009/2/10 namire <namire at gmail.com>:
> Hey .python first time poster here. I'm pretty good with python so
> far, but I keep needed a function in my program but not knowing how to
> build it. =( Here's the problem:
> Imagine a html file full of 100's of these strings all mooshed
> together onto many lines;
> <!--"@@MARKER@@; id=ITEM"-->ITEM<br>
> Where the word 'MARKER' is a constant, it stay the same in each string
> and the word 'ITEM' is a random combination of ascii characters of an
> unknown length. So for example a:
> <!--"@@MARKER@@; id=CATFISH"-->CATFISH<br><h1>Text text text</
> h1><!--"@@MARKER@@; id=SPAM"-->SPAM<br> and so on...
> What I need to do it replace each instance of the random letter with a
> constant and/or delete them.
> Just as a comparison in the Windows OS this seems easy to do when
> managing files, say for the file a-blah-b-blah.tmp where blah is an
> unknown you can use: del a-*-b-*.tmp to get rid of that file. But for
> python and a string in text file I don't have a clue. @_@ could
> someone please help me?
It is not quite clear to me, what should be achieved with the given
file, but an example with wildcard characters in windows implies, that
the regular expressions can be of some use here (given the file is as
regular as the samples, especially without nesting the comments etc.)
the segments in examples can be matched eg. with the expression>:
the ITEM, CATFISH, SPAM ... elements are captured in the parethesised
group and can be used foe matching or replacing.
check the re module in the python library:
More information about the Python-list