beginner - py unicode Q

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

    #1

    beginner - py unicode Q

    I read the posting by Rehceb Rotkiv and response but don't know if it
    relates to my problem in any way.

    I only want to write German to the screen/console for little German
    programs/exercises in python. No file w/r will be used.

    #! /usr/bin/env python
    # -*- coding: utf-8 -*-
    # Filename: 7P07png.py
    # SUSE Linux 10 Python 2.4.1 gedit 2.12.0

    print 'Ich zähle zwölf weiß Hüte.'
    print 'Wollen Sie'
    verbs = ( 'kömmen' , 'essen' , 'trinken' )
    print verbs[:3]

    print ' program ends '

    console display is: Ich zähle zwölf weiß Hüte.
    Wollen Sie
    ('k\xc3\xb6mmen ', 'essen', 'trinken')
    program ends

    The first 2 print statements in German print perfectly to screen/console
    but not the 3rd.

    I ran it with these lines below from Rehceb Rotkiv's code but it did not
    fix problem.
    import sys
    import codecs

    I also tried unicode string u'kömmen', but it did not fix problem.
    Any help/direction would be appreciated. Thanks in advance.

    I found this reference section but I am not sure it applies or how to
    use it to solve my problem.:

    This built in setdefaultencod ing(name) sets the default codec used to
    encode and decode Unicode and string objects (normally ascii)and is
    meant to be called only from sitecustomize.p y at program startup; the
    site module them removes this attribute from sys. You can call
    reload(sys) to make this attriute available again but this is not a good
    programming practice.

    I just thought of this. I suppose because this is py source code, it
    should not be German but a reference/key to u'strings' to print German
    text to the screen?

    The ultimate console output I seek, of course, using a while or for loop
    and/or random access for second verb, for example:
    Wollen Sie kömmen?
    Wollen Sie essen?
    Wollen Sie trinken?
  • John Machin

    #2
    Re: beginner - py unicode Q

    On Apr 8, 9:51 am, enquiring mind <braind...@brai ndead.comwrote:
    I read the posting by Rehceb Rotkiv and response but don't know if it
    relates to my problem in any way.
    >
    I only want to write German to the screen/console for little German
    programs/exercises in python. No file w/r will be used.
    >
    #! /usr/bin/env python
    # -*- coding: utf-8 -*-
    # Filename: 7P07png.py
    # SUSE Linux 10 Python 2.4.1 gedit 2.12.0
    >
    print 'Ich zähle zwölf weiß Hüte.'
    print 'Wollen Sie'
    verbs = ( 'kömmen' , 'essen' , 'trinken' )
    print verbs[:3]
    Note: the [:3] is redundant. "print verbs" would have the same effect.

    When you do print list_tuple_dict _etc, Python prints the repr() of
    each element. You are seeing repr('kömmen'). This is great for
    debugging, to see exactly what you've got (\xc3\xb6 is the utf8
    encoding for small o with diaeresis (aka umlaut)) but no so great for
    presentation to the user.

    To see the difference, insert here:
    for v in verbs:
    print v
    print str(v)
    print repr(v)
    >
    print ' program ends '
    >
    console display is: Ich zähle zwölf weiß Hüte.
    Wollen Sie
    ('k\xc3\xb6mmen ', 'essen', 'trinken')
    program ends
    >
    The first 2 print statements in German print perfectly to screen/console
    but not the 3rd.
    >
    I ran it with these lines below from Rehceb Rotkiv's code but it did not
    fix problem.
    import sys
    import codecs
    Importing modules without using them is pointless.
    >
    I also tried unicode string u'kömmen', but it did not fix problem.
    Any help/direction would be appreciated. Thanks in advance.
    >
    I found this reference section but I am not sure it applies or how to
    use it to solve my problem.:
    It doesn't solve your problem. Forget you ever read it.
    >
    This built in setdefaultencod ing(name) sets the default codec used to
    encode and decode Unicode and string objects (normally ascii)and is
    meant to be called only from sitecustomize.p y at program startup; the
    site module them removes this attribute from sys. You can call
    reload(sys) to make this attriute available again but this is not a good
    programming practice.
    >
    I just thought of this. I suppose because this is py source code, it
    should not be German but a reference/key to u'strings' to print German
    text to the screen?
    It's "German" only to a human who reads the console output and
    recognizes the bunches of characters as representing German words/
    phrases/sentences. Python and your computer see only utf8 encoding
    (which can be used to represent multiple languages all at once on the
    same screen or in the same paragraph of a document).

    Your console is quite happy rendering utf8 e.g. it printed "Ich zähle
    zwölf weiß Hüte" OK, didn't it? Try this:

    print "blahblah"
    print u"blahblah".enc ode('utf8')
    print u"blahblah"

    and see what happens.

    HTH,
    John

    Comment

    • Gabriel Genellina

      #3
      Re: beginner - py unicode Q

      enquiring mind wrote:
      I read the posting by Rehceb Rotkiv and response but don't know if it
      relates to my problem in any way.
      >
      I only want to write German to the screen/console for little German
      programs/exercises in python. No file w/r will be used.
      >
      #! /usr/bin/env python
      # -*- coding: utf-8 -*-
      # Filename: 7P07png.py
      # SUSE Linux 10 Python 2.4.1 gedit 2.12.0
      >
      print 'Ich zähle zwölf weiß Hüte.'
      print 'Wollen Sie'
      verbs = ( 'kömmen' , 'essen' , 'trinken' )
      print verbs[:3]
      >
      print ' program ends '
      >
      console display is: Ich zähle zwölf weiß Hüte.
      Wollen Sie
      ('k\xc3\xb6mmen ', 'essen', 'trinken')
      program ends
      >
      The first 2 print statements in German print perfectly to screen/console
      but not the 3rd.
      In the 3rd you don't print a string, you print a tuple. Python
      internally uses repr() on each item, joins them using "," and puts
      parenthesis around the whole thing.
      That's not usually what you want; that representation is good for
      debugging. (See below)
      I also tried unicode string u'kömmen', but it did not fix problem.
      Working in Unicode may be useful, but it's not your current problem.
      I found this reference section but I am not sure it applies or how to
      use it to solve my problem.:
      Forget about setdefaultencod ing!
      I just thought of this. I suppose because this is py source code, it
      should not be German but a reference/key to u'strings' to print German
      text to the screen?
      No...
      The ultimate console output I seek, of course, using a while or for loop
      and/or random access for second verb, for example:
      Wollen Sie kömmen?
      Wollen Sie essen?
      Wollen Sie trinken?
      Had you tried this before, you would have no problems.

      for verb in verbs:
      print 'Wollen Sie', verb, '?'

      This way you print an individual item, and you can control exactly how
      you like it displayed. Another way:

      for verb in verbs:
      print 'Wollen Sie %s?' % verb

      --
      Gabriel Genellina

      Comment

      Working...