From: Νίκος on
On 9 Αύγ, 21:05, Thomas Jollans <tho...(a)jollybox.de> wrote:
> On Monday 09 August 2010, it occurred to Νίκος to exclaim:
>
>
>
>
>
> > On 9 Αύγ, 19:21, Peter Otten <__pete...(a)web.de> wrote:
> > > Νίκος wrote:
> > > > Please tell me that no matter what weird charhs has inside ic an still
> > > > open thosie fiels and make the neccessary replacements.
>
> > > Go back to 2.6 for the moment and defer learning about unicode until
> > > you're done with the conversion job.
>
> > You are correct again! 3.2 caused the problem, i switched to 2.7 and
> > now i donyt have that problem anymore. File is openign okey!
>
> > it ALMOST convert correctly!
>
> > # replace tags
> > print ( 'replacing php tags and contents within' )
> > src_data = re.sub( '<\?(.*?)\?>', '', src_data )
>
> > it only convert the first instance of php tages and not the rest?
> > But why?
>
> http://docs.python.org/library/re.html#re.S
>
> You probably need to pass the re.DOTALL flag.

src_data = re.sub( '<\?(.*?)\?>', '', src_data, re.DOTALL )

like this?
From: Νίκος on
On 9 Áýã, 10:07, Íßêïò <nikos.the.gr...(a)gmail.com> wrote:
> Now the code looks as follows:
>
> =============================
> #!/usr/bin/python
>
> import re, os, sys
>
> id = 0  # unique page_id
>
> for currdir, files, dirs in os.walk('test'):
>
>         for f in files:
>
>                 if f.endswith('php'):
>
>                         # get abs path to filename
>                         src_f = join(currdir, f)
>
>                         # open php src file
>                         print ( 'reading from %s' % src_f )
>                         f = open(src_f, 'r')
>                         src_data = f.read()             # read contents of PHP file
>                         f.close()
>
>                         # replace tags
>                         print ( 'replacing php tags and contents within' )
>                         src_data = re.sub( '<?(..*?)?>', '', src_data )
>
>                         # add ID
>                         print ( 'adding unique page_id' )
>                         src_data = ( '<!-- %d -->' % id ) + src_data
>                         id += 1
>
>                         # add template variables
>                         print ( 'adding counter template variable' )
>                         src_data = src_data.replace('</body>', '<br><br><center><h4><font
> color=green> Áñéèìüò Åðéóêåðôþí: %(counter)d </body>' )
>
>                         # rename old php file to new with .html extension
>                         src_file = src_file.replace('.php', '.html')
>
>                         # open newly created html file for inserting data
>                         print ( 'writing to %s' % dest_f )
>                         dest_f = open(src_f, 'w')
>                         dest_f.write(src_data)          # write contents
>                         dest_f.close()
>
> I just tried to test it. I created a folder names 'test' in me 'd:\'
> drive.
> Then i have put to .php files inside form the original to test if it
> would work ok for those too files before acting in the whole copy and
> after in the original project.
>
> so i opened a 'cli' form my Win7 and tried
>
> D:\>convert.py
>
> D:\>
>
> Itsjust printed an empty line and nothign else. Why didn't even try to
> open the folder and fiels within?
> Syntactically it doesnt ghive me an error!
> Somehting with os.walk() methos perhaps?

Can you help in this too please?

Now iam able to just convrt a single file 'd:\test\index.php'

But these needs to be done for ALL the php files in every subfolder.

> for currdir, files, dirs in os.walk('test'):
>
>         for f in files:
>
>                 if f.endswith('php'):

Should the above lines enter folders and find php files in each folder
so to be edited?
From: Νίκος on
D:\>convert.py
File "D:\convert.py", line 34
SyntaxError: Non-ASCII character '\xce' in file D:\convert.py on line
34, but no
encoding declared; see http://www.python.org/peps/pep-0263.html for
details

D:\>

What does it refering too? what character cannot be identified?

Line 34 is:

src_data = src_data.replace( '</body>', '<br><br><center><h4><font
color=green> Áñéèìüò Åðéóêåðôþí: %(counter)d </body>' )

Also,

for currdir, files, dirs in os.walk('test'):

for f in files:

if f.lower().endswith("php"):

in the above lines

should i state os.walk('test') or os.walk('d:\test') ?
From: Νίκος on
Please help me with these last changes before i try to perform an
overall change.
its almost done!