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

Νίκος nikos.the.gr33k at gmail.com
Mon Aug 16 09:45:59 CEST 2010


On 10 Αύγ, 01:43, MRAB <pyt... at mrabarnett.plus.com> wrote:
> Íßêïò wrote:
> > 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; seehttp://www.python.org/peps/pep-0263.htmlfor
> > 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>' )
>
> Didn't you say that you're using Python 2.7 now? The default file
> encoding will be ASCII, but your file isn't ASCII, it contains Greek
> letters. Add the encoding line:
>
>      # -*- coding: utf-8 -*-
>
> and check that the file is saved as UTF-8.
>
> > Also,
>
> > for currdir, files, dirs in os.walk('test'):
>
> >    for f in files:

sctually its for currdir, dirs, filesin os.walk('test'):  thats whay
ti couldnt run!! :-)

After changifn this and made some other modification my convertion
script finally run!

Here it is for someone that might want a similar functionality.

======================================================================

#!/usr/bin/python
# -*- coding: utf-8 -*-

import re, os, sys


count = 520

for currdir, dirs, files in os.walk('d:\\akis'):

	for f in files:

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

			# get abs path to filename
			src_f = os.path.join(currdir, f)

			# open php src file
			f = open(src_f, 'r')
			src_data = f.read()
			f.close()

			# Grab the id number contained within the php code and insert it
above all other data
			found = re.search( r'PageID = (\d+)', src_data )
			if found:
				id = found.group(1)
			else:
				id = count =+ 1
			src_data = ( '<!-- %s -->\n\n' % id ) + src_data

			# replace php tags and contents within
			src_data = re.sub( r'(?s)<\?(.*?)\?>', '', src_data )

			# add template variables
			src_data = src_data.replace( '</body>', '<br><br><center><h4><font
color=green> Αριθμός Επισκεπτών: %(counter)d </body>' )

			# open same php file for storing modified data
			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 )
			print ( "renaming: %s => %s\n" % (src_f, dst_f) )





More information about the Python-list mailing list