WTF? Printing unicode strings

Ron Garret rNOSPAMon at flownet.com
Thu May 18 19:12:10 EDT 2006


In article <1147992722.970761.220840 at j73g2000cwa.googlegroups.com>,
 "Serge Orlov" <Serge.Orlov at gmail.com> wrote:

> Ron Garret wrote:
> > In article <mailman.5906.1147989402.27775.python-list at python.org>,
> >  Robert Kern <robert.kern at gmail.com> wrote:
> >
> > > Ron Garret wrote:
> > >
> > > > I forgot to mention:
> > > >
> > > >>>>sys.getdefaultencoding()
> > > >
> > > > 'utf-8'
> > >
> > > A) You shouldn't be able to do that.
> >
> > What can I say?  I can.
> >
> > > B) Don't do that.
> >
> > OK.  What should I do instead?
> 
> Exact answer depends on what OS and terminal you are using and what
> your program is supposed to do, are you going to distribute the program
> or it's just for internal use.

I'm using an OS X terminal to ssh to a Linux machine.

But what about this:

>>> f2=open('foo','w')
>>> f2.write(u'\xFF')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
UnicodeEncodeError: 'ascii' codec can't encode character u'\xff' in 
position 0: ordinal not in range(128)
>>> 

That should have nothing to do with my terminal, right?

I just found http://www.amk.ca/python/howto/unicode, which seems to be 
enlightening.  The answer seems to be something like:

import codecs
f = codecs.open('foo','w','utf-8')

but that seems pretty awkward.

rg



More information about the Python-list mailing list