Replace and inserting strings within .txt files with the use of regex
Νίκος
nikos.the.gr33k at gmail.com
Wed Aug 11 05:48:22 EDT 2010
On 10 Αύγ, 18:12, MRAB <pyt... at mrabarnett.plus.com> wrote:
> Νίκος wrote:
>
> [snip]
>
>
>
>
>
> > The ID number of each php page was contained in the old php code
> > within this string
>
> > PageID = some_number
>
> > So instead of create a new ID number for eaqch page i have to pull out
> > this number to store to the beginnign to the file as comment line,
> > because it has direct relationship with the mysql database as in
> > tracking the number of each webpage and finding the counter of it.
>
> > # Grab the PageID contained within the php code and store it in id
> > variable
> > id = re.search( 'PageID = ', src_data )
>
> > How to tell Python to Grab that number after 'PageID = ' string and to
> > store it in var id that a later use in the program?
>
> If the part of the file you're trying to match look like this:
>
> PageID = 12
>
> then the regex should look like this:
>
> PageID = (\d+)
>
> and the code should look like this:
>
> page_id = re.search(r'PageID = (\d+)', src_data).group(1)
>
> The page_id will, of course, be a string.
>
Thank you very much for helping me with the syntax.
> > also i made another changewould something like this work:
>
> > ===============================
> > # open same php file for storing modified data
> > print ( 'writing to %s' % dest_f )
> > f = open(src_f, 'w')
> > f.write(src_data)
> > f.close()
>
> > # rename edited .php file to .html extension
> > dst_f = src_f.replace('.php', '.html')
> > os.rename( src_f, dst_f )
> > ===============================
>
> > Because instead of creating a new .html file and inserting the desired
> > data of the old php thus having two files(old php, and new html) i
> > decided to open the same php file for writing that data and then
> > rename it to html.
> > Would the above code work?
>
> Why wouldn't it?
I though i was perhaps did something wrong with the code.
=========================================
for currdir, files, dirs in os.walk('d:\\test'): # neither 'd:/test'
tracks the folder
for f in files:
if f.lower().endswith("php"):
print currdir, files, dirs, f
=========================================
As you advised me in a previous post of yours i need to find out why
the converting code
although works for a single file doesn't for some reason enter folders
and subfolders to grab files form there to convert.
So as you said i should comment all other statements to find out the
culprit in the above lines.
Well those lines are supposed to print current working folder and
files but when i run the above code it gives me nothing in response,
not even 'f'.
So does that mean that os.walk() method cannot enter the windows 7
folders?
* One more thing is that instead of trying to run the above script
form 'cli' wouldn't it better to run it as a cgi script and see the
results in the browser instead with the addition fo this line?
print ( "Content-type: text/html; charset=UTF-8 \n" )
Or for some reason this has to be run from the shell to both
local(windows 7) and remote hosting (linux) servers?
More information about the Python-list
mailing list