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

Peter Otten __peter__ at web.de
Mon Aug 9 12:47:22 CEST 2010


Νίκος wrote:

> On 9 Αύγ, 13:06, Peter Otten <__pete... at web.de> wrote:
> 
>> > So since its utf-8 what the problem of opening it?
>>
>> Python says it's not, and I tend to believe it.
> 
> You are right!
> 
> I tried to do the same exact openign via IDLE enviroment and i goth
> the encoding of the file from there!
> 
>>>> open("d:\\test\\index.php" ,'r')
> <_io.TextIOWrapper name='d:\\test\\index.php' encoding='cp1253'>
> 
> Thats why in the error in my previous post it said
> File "C:\Python32\lib\encodings\cp1253.py", line 23, in decode
> it tried to use the cp1253 encoding.
> 
> But now sicne Python as we see can undestand the nature of the
> encoding what causing it not to open the file?

It doesn't. You have to tell. *If* the file uses cp1253 you can open it with

open(..., encoding="cp1253")

Note that if the file is not in cp1253 python will still happily open it as 
long as it doesn't contain the following bytes:


>>> for i in range(256):
...     try: chr(i).decode("cp1253") and None
...     except: print i
...
129
136
138
140
141
142
143
144
152
154
156
157
158
159
170
210
255

Peter




More information about the Python-list mailing list