Replace and inserting strings within .txt files with the use of regex

MRAB python at mrabarnett.plus.com
Sat Aug 7 21:42:07 EDT 2010


rantingrick wrote:
> On Aug 7, 7:20 pm, Νίκος <nikos.the.gr... at gmail.com> wrote:
>> Hello dear Pythoneers,
> 
> I prefer Pythonista, but anywho..
> 
>> I have over 500 .php web pages in various subfolders under 'data'
>> folder that i have to rename to .html
> 
> import os
> os.rename(old, new)
> 
>> and and ditch the '<?' and '?>' tages from within
> 
> path = 'some/valid/path'
> f = open(path, 'r')
> data = f.read()
> f.close()
> data.replace('<?', '')
> data.replace('?>', '')
> 
That should be:

   data = data.replace('<?', '')
   data = data.replace('?>', '')

>>  and also insert a very first line of <!-- id -->
>> where id must be an identification unique number of every page for
>> counter tracking purposes.
> 
> comment = "<!-- %s -->"%(idnum)
> data.insert(idx, comment)
> 
Strings don't have an 'insert' method!

>> ONly pure html code must be left.
> 
> Well then don't F up! However judging from the amount of typos in this
> post i would suggest you do some major testing!
> 
>> I don't know how to handle such a big data replacing problem and
>> cannot play with fire because those 500 pages are my cleints pages and
>> data of those files just cannot be messes up.
> 
> Better do some serous testing first, or (if you have enough disc
> space ) create copies instead!
> 
>> Can you provide to me a script please that is able of performing an
>> automatic way of such a page content replacing?
> 
> This is very basic stuff and the fine manual is free you know. But how
> much are you willing to pay?




More information about the Python-list mailing list