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

MRAB python at mrabarnett.plus.com
Mon Aug 9 18:43:17 EDT 2010


Νίκος 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; 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>' )
> 
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:
> 
> 		if f.lower().endswith("php"):
> 
> in the above lines
> 
> should i state  os.walk('test') or  os.walk('d:\test') ?

The path 'test' is relative to the current working directory. Is that
D:\ for your script? If not, then it won't find the (correct) folder.

It might be better to use an absolute path instead. You could use
either:

     r'd:\test'

(note that I've made it a raw string because it contains a backslash
which I want treated as a literal backslash) or:

     'd:/test'

(Windows should accept a slash as well as of a backslash.)



More information about the Python-list mailing list