Unicode, command-line and idle

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • a.serrano@vielca.com

    #1

    Unicode, command-line and idle

    Hello, the following program prompts the user for a word and tests to
    see if it is the same as another one. If the user types "españa" (note
    that the word contains an 'ñ'), the program should output "same". This
    works if I run the code in IDLE but does not if I run it in the windows
    console. Can someone explain me why this happens and how to get around
    it?

    # -*- coding: cp1252 -*-
    text1 = 'españa'
    text2 = raw_input()
    if text1 == text2:
    print 'same'
    else:
    print 'not same'

  • Fredrik Lundh

    #2
    Re: Unicode, command-line and idle

    a.serrano@vielc a.com wrote:
    [color=blue]
    > Hello, the following program prompts the user for a word and tests to
    > see if it is the same as another one. If the user types "españa" (note
    > that the word contains an 'ñ'), the program should output "same". This
    > works if I run the code in IDLE but does not if I run it in the windows
    > console. Can someone explain me why this happens and how to get around
    > it?
    >
    > # -*- coding: cp1252 -*-
    > text1 = 'españa'
    > text2 = raw_input()
    > if text1 == text2:
    > print 'same'
    > else:
    > print 'not same'[/color]

    I don't think raw_input decodes non-ascii input at all. and your console
    probably uses cp850 rather than cp1252. doing the comparision over in
    unicode space should work better:

    import sys
    text1 = u'españa'
    text2 = unicode(raw_inp ut(), sys.stdin.encod ing)
    if text1 == text2:
    print 'same'
    else:
    print 'not same'

    </F>



    Comment

    • Méta-MCI

      #3
      Re: Unicode, command-line and idle

      (just for confirm)


      Hi!


      if the console is in cp1252, raw_input work OK with "ñ"

      This (your) script :


      # -*- coding: cp1252 -*-

      import sys
      text1 = u'españa'
      text2 = unicode(raw_inp ut(), sys.stdin.encod ing)
      if text1 == text2:
      print 'same'
      else:
      print 'not same'


      work OK with "chcp 850" AND "chcp 1252"





      @-salutations

      Michel Claveau



      Comment

      • a.serrano@vielca.com

        #4
        Re: Unicode, command-line and idle

        Fredrik Lundh wrote:[color=blue]
        > a.serrano@vielc a.com wrote:
        >[color=green]
        > > Hello, the following program prompts the user for a word and tests to
        > > see if it is the same as another one. If the user types "españa" (note
        > > that the word contains an 'ñ'), the program should output "same". This
        > > works if I run the code in IDLE but does not if I run it in the windows
        > > console. Can someone explain me why this happens and how to get around
        > > it?
        > >
        > > # -*- coding: cp1252 -*-
        > > text1 = 'españa'
        > > text2 = raw_input()
        > > if text1 == text2:
        > > print 'same'
        > > else:
        > > print 'not same'[/color]
        >
        > I don't think raw_input decodes non-ascii input at all. and your console
        > probably uses cp850 rather than cp1252. doing the comparision over in
        > unicode space should work better:
        >
        > import sys
        > text1 = u'españa'
        > text2 = unicode(raw_inp ut(), sys.stdin.encod ing)
        > if text1 == text2:
        > print 'same'
        > else:
        > print 'not same'
        >
        > </F>[/color]

        This works if I use the console but gives the following error if I use
        IDLE:

        Traceback (most recent call last):
        File "C:\test.py ", line 4, in ?
        text2 = unicode(raw_inp ut(), sys.stdin.encod ing)
        AttributeError: PyShell instance has no attribute 'encoding'

        Comment

        • Martin v. Löwis

          #5
          Re: Unicode, command-line and idle

          a.serrano@vielc a.com wrote:[color=blue]
          > This works if I use the console but gives the following error if I use
          > IDLE:
          >
          > Traceback (most recent call last):
          > File "C:\test.py ", line 4, in ?
          > text2 = unicode(raw_inp ut(), sys.stdin.encod ing)
          > AttributeError: PyShell instance has no attribute 'encoding'[/color]

          What operating system and Python version are you using? This ought to
          work.

          Regards,
          Martin

          Comment

          • John Machin

            #6
            Re: Unicode, command-line and idle

            The docs say that the encoding attribute is "New in version 2.3".
            Python 2.2's IDLE produces exactly the exception reported by the OP.
            Cheers,
            John

            Comment

            • a.serrano@vielca.com

              #7
              Re: Unicode, command-line and idle


              Martin v. Löwis wrote:[color=blue]
              > a.serrano@vielc a.com wrote:[color=green]
              > > This works if I use the console but gives the following error if I use
              > > IDLE:
              > >
              > > Traceback (most recent call last):
              > > File "C:\test.py ", line 4, in ?
              > > text2 = unicode(raw_inp ut(), sys.stdin.encod ing)
              > > AttributeError: PyShell instance has no attribute 'encoding'[/color]
              >
              > What operating system and Python version are you using? This ought to
              > work.
              >
              > Regards,
              > Martin[/color]

              Python 2.4.3
              IDLE 1.1.3
              Windows XP SP2

              Comment

              • Kent Johnson

                #8
                Re: Unicode, command-line and idle

                a.serrano@vielc a.com wrote:[color=blue]
                > Martin v. Löwis wrote:[color=green]
                >> a.serrano@vielc a.com wrote:[color=darkred]
                >>> This works if I use the console but gives the following error if I use
                >>> IDLE:
                >>>
                >>> Traceback (most recent call last):
                >>> File "C:\test.py ", line 4, in ?
                >>> text2 = unicode(raw_inp ut(), sys.stdin.encod ing)
                >>> AttributeError: PyShell instance has no attribute 'encoding'[/color]
                >> What operating system and Python version are you using? This ought to
                >> work.
                >>
                >> Regards,
                >> Martin[/color]
                >
                > Python 2.4.3
                > IDLE 1.1.3
                > Windows XP SP2[/color]

                ?? Works for me on Win2K

                Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)] on
                win32
                Type "copyright" , "credits" or "license()" for more information.

                IDLE 1.1.3[color=blue][color=green][color=darkred]
                >>> import sys
                >>> sys.stdin.encod ing[/color][/color][/color]
                'cp1252'

                Kent

                Comment

                • a.serrano@vielca.com

                  #9
                  Re: Unicode, command-line and idle

                  Hello again, I've investigated a little bit and this is what I found:

                  If I run IDLE and type
                  [color=blue][color=green][color=darkred]
                  >>> import sys
                  >>> sys.stdin.encod ing[/color][/color][/color]

                  I get

                  'cp1252'

                  But if I have a whatever.py file (it can even be a blank file), I edit
                  it with IDLE, I press F5 (Run Module) and then type:
                  [color=blue][color=green][color=darkred]
                  >>> import sys
                  >>> sys.stdin.encod ing[/color][/color][/color]

                  I get

                  Traceback (most recent call last):
                  File "<pyshell#1 >", line 1, in ?
                  sys.stdin.encod ing
                  AttributeError: PyShell instance has no attribute 'encoding'

                  So when I have the following code in a file:

                  # -*- coding: cp1252 -*-
                  import sys
                  text1 = u'españa'
                  text2 = unicode(raw_inp ut(), sys.stdin.encod ing)
                  if text1 == text2:
                  print 'same'
                  else:
                  print 'not same'

                  and I press F5 (Run Module) I get:

                  Traceback (most recent call last):
                  File "C:\test.py ", line 4, in ?
                  text2 = unicode(raw_inp ut(), sys.stdin.encod ing)
                  AttributeError: PyShell instance has no attribute 'encoding'

                  This same code works if I just double-click it (run it in the windows
                  console) instead of using IDLE.

                  I'm using Python 2.4.3 and IDLE 1.1.3.

                  Armando.

                  Comment

                  • Kent Johnson

                    #10
                    Re: Unicode, command-line and idle

                    a.serrano@vielc a.com wrote:[color=blue]
                    > Hello again, I've investigated a little bit and this is what I found:
                    >
                    > If I run IDLE and type
                    >[color=green][color=darkred]
                    >>>> import sys
                    >>>> sys.stdin.encod ing[/color][/color]
                    >
                    > I get
                    >
                    > 'cp1252'
                    >
                    > But if I have a whatever.py file (it can even be a blank file), I edit
                    > it with IDLE, I press F5 (Run Module) and then type:
                    >[color=green][color=darkred]
                    >>>> import sys
                    >>>> sys.stdin.encod ing[/color][/color]
                    >
                    > I get
                    >
                    > Traceback (most recent call last):
                    > File "<pyshell#1 >", line 1, in ?
                    > sys.stdin.encod ing
                    > AttributeError: PyShell instance has no attribute 'encoding'
                    >
                    > So when I have the following code in a file:
                    >
                    > # -*- coding: cp1252 -*-
                    > import sys
                    > text1 = u'españa'
                    > text2 = unicode(raw_inp ut(), sys.stdin.encod ing)
                    > if text1 == text2:
                    > print 'same'
                    > else:
                    > print 'not same'
                    >
                    > and I press F5 (Run Module) I get:
                    >
                    > Traceback (most recent call last):
                    > File "C:\test.py ", line 4, in ?
                    > text2 = unicode(raw_inp ut(), sys.stdin.encod ing)
                    > AttributeError: PyShell instance has no attribute 'encoding'
                    >
                    > This same code works if I just double-click it (run it in the windows
                    > console) instead of using IDLE.
                    >
                    > I'm using Python 2.4.3 and IDLE 1.1.3.[/color]

                    FWIW all of the above give me 'cp1252', not AttributeError, and the type
                    of sys.stdin on my system is idlelib.rpc.RPC Proxy, not PyShell.

                    Python 2.4.3 and IDLE 1.1.3 on Win2k

                    Kent

                    Comment

                    • Egon Frerich

                      #11
                      Re: Unicode, command-line and idle

                      -----BEGIN PGP SIGNED MESSAGE-----
                      Hash: SHA1

                      What do you have in the IDLE options - General - Default source encoding?

                      Egon

                      Kent Johnson schrieb am 12.04.2006 12:40:
                      [color=blue]
                      > a.serrano@vielc a.com wrote:[color=green]
                      >> Hello again, I've investigated a little bit and this is what I found:
                      >>
                      >> If I run IDLE and type
                      >>[color=darkred]
                      >>>>> import sys
                      >>>>> sys.stdin.encod ing[/color]
                      >> I get
                      >>
                      >> 'cp1252'
                      >>
                      >> But if I have a whatever.py file (it can even be a blank file), I edit
                      >> it with IDLE, I press F5 (Run Module) and then type:
                      >>[color=darkred]
                      >>>>> import sys
                      >>>>> sys.stdin.encod ing[/color]
                      >> I get
                      >>
                      >> Traceback (most recent call last):
                      >> File "<pyshell#1 >", line 1, in ?
                      >> sys.stdin.encod ing
                      >> AttributeError: PyShell instance has no attribute 'encoding'
                      >>
                      >> So when I have the following code in a file:
                      >>
                      >> # -*- coding: cp1252 -*-
                      >> import sys
                      >> text1 = u'españa'
                      >> text2 = unicode(raw_inp ut(), sys.stdin.encod ing)
                      >> if text1 == text2:
                      >> print 'same'
                      >> else:
                      >> print 'not same'
                      >>
                      >> and I press F5 (Run Module) I get:
                      >>
                      >> Traceback (most recent call last):
                      >> File "C:\test.py ", line 4, in ?
                      >> text2 = unicode(raw_inp ut(), sys.stdin.encod ing)
                      >> AttributeError: PyShell instance has no attribute 'encoding'
                      >>
                      >> This same code works if I just double-click it (run it in the windows
                      >> console) instead of using IDLE.
                      >>
                      >> I'm using Python 2.4.3 and IDLE 1.1.3.[/color]
                      >
                      > FWIW all of the above give me 'cp1252', not AttributeError, and the type
                      > of sys.stdin on my system is idlelib.rpc.RPC Proxy, not PyShell.
                      >
                      > Python 2.4.3 and IDLE 1.1.3 on Win2k
                      >
                      > Kent[/color]

                      - --
                      Egon Frerich, Freudenbergstr. 16, 28213 Bremen

                      E-Mail: e.frerich@nord-com.net
                      -----BEGIN PGP SIGNATURE-----
                      Version: GnuPG v1.4.2.2 (MingW32)
                      Comment: GnuPT 2.7.2
                      Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

                      iD8DBQFEPOpuuTz ybIiyjvURAqKTAJ 9omGK03L9p5dHpz jqN9Kz1w6cTYACg hO6r
                      VG30LibkskG9M2b oF/lTc0s=
                      =Xl96
                      -----END PGP SIGNATURE-----

                      Comment

                      • Kent Johnson

                        #12
                        Re: Unicode, command-line and idle

                        Egon Frerich wrote:[color=blue]
                        > -----BEGIN PGP SIGNED MESSAGE-----
                        > Hash: SHA1
                        >
                        > What do you have in the IDLE options - General - Default source encoding?[/color]

                        UTF-8
                        [color=blue]
                        >
                        > Egon
                        >
                        > Kent Johnson schrieb am 12.04.2006 12:40:
                        >[color=green]
                        >> a.serrano@vielc a.com wrote:[color=darkred]
                        >>> Hello again, I've investigated a little bit and this is what I found:
                        >>>
                        >>> If I run IDLE and type
                        >>>
                        >>>>>> import sys
                        >>>>>> sys.stdin.encod ing
                        >>> I get
                        >>>
                        >>> 'cp1252'
                        >>>
                        >>> But if I have a whatever.py file (it can even be a blank file), I edit
                        >>> it with IDLE, I press F5 (Run Module) and then type:
                        >>>
                        >>>>>> import sys
                        >>>>>> sys.stdin.encod ing
                        >>> I get
                        >>>
                        >>> Traceback (most recent call last):
                        >>> File "<pyshell#1 >", line 1, in ?
                        >>> sys.stdin.encod ing
                        >>> AttributeError: PyShell instance has no attribute 'encoding'
                        >>>
                        >>> So when I have the following code in a file:
                        >>>
                        >>> # -*- coding: cp1252 -*-
                        >>> import sys
                        >>> text1 = u'españa'
                        >>> text2 = unicode(raw_inp ut(), sys.stdin.encod ing)
                        >>> if text1 == text2:
                        >>> print 'same'
                        >>> else:
                        >>> print 'not same'
                        >>>
                        >>> and I press F5 (Run Module) I get:
                        >>>
                        >>> Traceback (most recent call last):
                        >>> File "C:\test.py ", line 4, in ?
                        >>> text2 = unicode(raw_inp ut(), sys.stdin.encod ing)
                        >>> AttributeError: PyShell instance has no attribute 'encoding'
                        >>>
                        >>> This same code works if I just double-click it (run it in the windows
                        >>> console) instead of using IDLE.
                        >>>
                        >>> I'm using Python 2.4.3 and IDLE 1.1.3.[/color]
                        >> FWIW all of the above give me 'cp1252', not AttributeError, and the type
                        >> of sys.stdin on my system is idlelib.rpc.RPC Proxy, not PyShell.
                        >>
                        >> Python 2.4.3 and IDLE 1.1.3 on Win2k
                        >>
                        >> Kent[/color]
                        >
                        > - --
                        > Egon Frerich, Freudenbergstr. 16, 28213 Bremen
                        >
                        > E-Mail: e.frerich@nord-com.net
                        > -----BEGIN PGP SIGNATURE-----
                        > Version: GnuPG v1.4.2.2 (MingW32)
                        > Comment: GnuPT 2.7.2
                        > Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
                        >
                        > iD8DBQFEPOpuuTz ybIiyjvURAqKTAJ 9omGK03L9p5dHpz jqN9Kz1w6cTYACg hO6r
                        > VG30LibkskG9M2b oF/lTc0s=
                        > =Xl96
                        > -----END PGP SIGNATURE-----
                        >
                        >
                        >[/color]


                        Comment

                        Working...