I had a similar problem but i can 't encode a byte to a file what has been uploaded, without damage the data if i used utf-8 to encode the file duplicates the size, and i try to change the codec to raw_unicode_escape and this barely give me the correct size but still damage the file, i used Python 3 and i have to encode the file again.<br /><br />On Oct 9, 2010 11:39pm, Chris Rebert <crebert@ucsd.edu> wrote:<br />> On Sat, Oct 9, 2010 at 4:59 PM, Brian Blais bblais@bryant.edu> wrote:<br />> <br />> > This may be a stemming from my complete ignorance of unicode, but when I do this (Python 2.6):<br />> <br />> ><br />> <br />> > s='\xc2\xa9 2008 \r\n'<br />> <br />> ><br />> <br />> > and I want the ascii version of it, ignoring any non-ascii chars, I thought I could do:<br />> <br />> ><br />> <br />> > s.encode('ascii','ignore')<br />> <br />> ><br />> <br />> > but it gives the error:<br />> <br />> ><br />> <br />> > In [20]:s.encode('ascii','ignore')<br />> <br />> > ----------------------------------------------------------------------------<br />> <br />> > UnicodeDecodeError                        Traceback (most recent call last)<br />> <br />> ><br />> <br />> > /Users/bblais/python/doit100810a.py in ()<br />> <br />> > ----> 1<br />> <br />> >      2<br />> <br />> >      3<br />> <br />> >      4<br />> <br />> >      5<br />> <br />> ><br />> <br />> > UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 0: ordinal not in range(128)<br />> <br />> ><br />> <br />> > am I doing something stupid here?<br />> <br />> <br />> <br />> In addition to Benjamin's explanation:<br />> <br />> <br />> <br />> Unicode strings in Python are of type `unicode` and written with a<br />> <br />> leading "u"; e.g. u"A unicode string for ¥500". Byte strings lack the<br />> <br />> leading "u"; e.g. "A plain byte string". Note that "Unicode string"<br />> <br />> does not refer to strings which have been encoded using a Unicode<br />> <br />> encoding (e.g. UTF-8); such strings are still byte strings, for<br />> <br />> encodings emit bytes.<br />> <br />> <br />> <br />> As to why you got the /exact/ error you did:<br />> <br />> As a backward compatibility hack, in order to satisfy your nonsensical<br />> <br />> encoding request, Python implicitly tried to decode the byte string<br />> <br />> `s` using ASCII as a default (the choice of ASCII here has nothing to<br />> <br />> do with the fact that you specified ASCII in your encoding request),<br />> <br />> so that it could then try and encode the resulting unicode string;<br />> <br />> hence why you got a Unicode*De*codeError as opposed to a<br />> <br />> Unicode*En*codeError, despite the fact you called *en*code().<br />> <br />> <br />> <br />> Highly suggested further reading:<br />> <br />> "The Absolute Minimum Every Software Developer Absolutely, Positively<br />> <br />> Must Know About Unicode and Character Sets (No Excuses!)"<br />> <br />> http://www.joelonsoftware.com/articles/Unicode.html<br />> <br />> <br />> <br />> Cheers,<br />> <br />> Chris<br />> <br />> --<br />> <br />> http://mail.python.org/mailman/listinfo/python-list<br />>