Encodings and printing unicode

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fuzzyman

    #1

    Encodings and printing unicode

    How does the print statement decode unicode strings itis passed ? (By
    that I mean which encoding does it use).

    Under windows it doesn't appear to use defaultencoding . On my system
    the default encoding is ascii, yet the terminal encoding is latin1 (or
    cp1252 or whatever, but not ascii). This means that print '£' works
    fine, yet unicode('£') will raise the UnicodeDecodeEr ror.

    However print u'£' will also work fine. (Under pythonce this same
    statement raises a UnicodeDecodeEr ror - but the 'terminal' is
    different).

    In my understanding unicode is an 'internal representation' - if you
    want to write a unicode string to a file you have to turn it into a
    byte string and specify an encoding (unless you pickle it - which is
    cheating). So when you 'print' a unicode string, what is sent to
    sys.stdout ? Is a character encoding used ? If so which one... if
    not... why not ?

    (In this case if defaultencoding was used it *ought* to raise a
    UnicodeDecodeEr ror).

    Regards,

    Fuzzy
    http://www.voidspace.org.uk/python/index.shtml

  • Kent Johnson

    #2
    Re: Encodings and printing unicode

    Fuzzyman wrote:[color=blue]
    > How does the print statement decode unicode strings itis passed ? (By
    > that I mean which encoding does it use).[/color]

    sys.stdout.enco ding
    [color=blue]
    > Under windows it doesn't appear to use defaultencoding . On my system
    > the default encoding is ascii, yet the terminal encoding is latin1 (or
    > cp1252 or whatever, but not ascii). This means that print '£' works
    > fine, yet unicode('£') will raise the UnicodeDecodeEr ror.[/color]

    This uses sys.defaultenco ding

    Kent

    Comment

    • Fuzzyman

      #3
      Re: Encodings and printing unicode


      Kent Johnson wrote:[color=blue]
      > Fuzzyman wrote:[color=green]
      > > How does the print statement decode unicode strings itis passed ?[/color][/color]
      (By[color=blue][color=green]
      > > that I mean which encoding does it use).[/color]
      >
      > sys.stdout.enco ding
      > [snip..][/color]

      Aha... that's the missing piece of information. Thank you.

      Regards,

      Fuzzy
      http://www.voidspace.org.uk/python/index.shtml

      Comment

      Working...