file.encoding doesn't apply to file.write?

Discussion in 'Python' started by Matthew Mueller, Jun 7, 2004.

  1. I noticed in python2.3 printing unicode to an appropriate terminal
    actually works. But using sys.stdout.write doesn't.

    Ex:
    Python 2.3.4 (#2, May 29 2004, 03:31:27)
    [GCC 3.3.3 (Debian 20040417)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> sys.stdout.encoding

    'UTF-8'
    >>> u=u'\u3053\u3093\u306b\u3061\u308f'
    >>> print u

    ã“ã‚“ã«ã¡ã‚
    >>> sys.stdout.write(u)

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)


    The file object docs say:
    "encoding
    The encoding that this file uses. When Unicode strings are written to
    a file, they will be converted to byte strings using this encoding.
    ..."
    Which indicates to me that it is supposed to work.

    Of course, I could use print >>fileobj, but that is ugly ;)


    -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
    http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
    -----== Over 100,000 Newsgroups - 19 Different Servers! =-----
     
    Matthew Mueller, Jun 7, 2004
    #1
    1. Advertising

  2. Matthew Mueller wrote:
    > I noticed in python2.3 printing unicode to an appropriate terminal
    > actually works. But using sys.stdout.write doesn't.


    Please report that as a bug. As a work-around, explicitly encode
    with sys.stdout.encoding (or make a codecs.StreamReaderWriter,
    passing codecs.lookup(sys.stdout.encoding)).

    Regards,
    Martin
     
    =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?=, Jun 7, 2004
    #2
    1. Advertising

  3. On Mon, 07 Jun 2004 07:55:00 +0200, Martin v. Löwis wrote:

    > Matthew Mueller wrote:
    >> I noticed in python2.3 printing unicode to an appropriate terminal
    >> actually works. But using sys.stdout.write doesn't.

    >
    > Please report that as a bug. As a work-around, explicitly encode
    > with sys.stdout.encoding (or make a codecs.StreamReaderWriter,
    > passing codecs.lookup(sys.stdout.encoding)).


    I submitted a bug(https://sourceforge.net/tracker/?group_id=5470&atid=105470)

    And I'm trying using StreamWriter, which I may actually want anyway so I
    can set the error handling. But I've ran into a weird thing. Some codecs
    don't like writing strings, only unicode. This is problematic because it
    means I can't just use the StreamWriter as a drop in replacement for
    stdout, etc:

    Python 2.3.4 (#2, May 29 2004, 03:31:27)
    [GCC 3.3.3 (Debian 20040417)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import codecs
    >>> sys.stdout=codecs.getwriter('UTF-8')(sys.__stdout__)
    >>> print 'hello'

    hello
    >>> sys.stdout=codecs.getwriter('EUC-JP')(sys.__stdout__)
    >>> print 'hello'

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: only unicode objects are encodable.




    -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
    http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
    -----== Over 100,000 Newsgroups - 19 Different Servers! =-----
     
    Matthew Mueller, Jun 7, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    23,375
    Real Gagnon
    Oct 8, 2004
  2. Stefan Siegl
    Replies:
    1
    Views:
    965
    Marrow
    Jul 18, 2003
  3. littlebeam
    Replies:
    2
    Views:
    834
    Egbert Nierop \(MVP for IIS\)
    Apr 12, 2006
  4. Dave Griffith

    Image::Magick->Write() doesn't want to write

    Dave Griffith, Nov 16, 2009, in forum: Perl Misc
    Replies:
    2
    Views:
    231
    Dave Griffith
    Nov 16, 2009
Loading...

Share This Page