about coding

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

    #1

    about coding

    Hi, being a newbie in Python, I'm a bit lost with the '-*- coding : -*-'
    directive.

    I'm using an accented characters language. Some of them are correctly
    displayed while one doesn't. I've written :
    -*- coding: utf-8 -*-

    Is this wrong ?

    Where can I find a pratical explanation about these encodings ?

    Thanks.
  • Robert Kern

    #2
    Re: about coding

    cantabile wrote:[color=blue]
    > Hi, being a newbie in Python, I'm a bit lost with the '-*- coding : -*-'
    > directive.
    >
    > I'm using an accented characters language. Some of them are correctly
    > displayed while one doesn't. I've written :
    > -*- coding: utf-8 -*-
    >
    > Is this wrong ?[/color]

    It depends. Are those characters encoded as UTF-8? Or, more likely, are
    they encoded as ISO-8859-1?
    [color=blue]
    > Where can I find a pratical explanation about these encodings ?[/color]



    Ever wonder about that mysterious Content-Type tag? You know, the one you’re supposed to put in HTML and you never quite know what it should be? Did you ever get an email from your friends in…


    --
    Robert Kern
    rkern@ucsd.edu

    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter

    Comment

    • Benjamin Niemann

      #3
      Re: about coding

      cantabile wrote:
      [color=blue]
      > Hi, being a newbie in Python, I'm a bit lost with the '-*- coding : -*-'
      > directive.
      >
      > I'm using an accented characters language. Some of them are correctly
      > displayed while one doesn't. I've written :
      > -*- coding: utf-8 -*-
      >
      > Is this wrong ?
      >
      > Where can I find a pratical explanation about these encodings ?[/color]

      The coding line tell the interpreter to assume that the file is encoded as
      utf-8. It's the job of the editor that you are using, to actually encode
      the file as utf-8.
      The coding directive uses a format that is automatically recognized by the
      emacs editor. If you are using another editor, you'll have to search its
      menus for the proper way to save files as utf-8.


      --
      Benjamin Niemann
      Email: pink at odahoda dot de
      WWW: http://www.odahoda.de/

      Comment

      • John Machin

        #4
        Re: about coding

        Benjamin Niemann wrote:[color=blue]
        > cantabile wrote:
        >
        >[color=green]
        >>Hi, being a newbie in Python, I'm a bit lost with the '-*- coding : -*-'
        >>directive.
        >>
        >>I'm using an accented characters language. Some of them are correctly
        >>displayed while one doesn't. I've written :
        >>-*- coding: utf-8 -*-
        >>
        >>Is this wrong ?
        >>
        >>Where can I find a pratical explanation about these encodings ?[/color]
        >
        >
        > The coding line tell the interpreter to assume that the file is encoded as
        > utf-8. It's the job of the editor that you are using, to actually encode
        > the file as utf-8.
        > The coding directive uses a format that is automatically recognized by the
        > emacs editor. If you are using another editor, you'll have to search its
        > menus for the proper way to save files as utf-8.
        >
        >[/color]

        or possibly the OP needs to nut out what is the encoding he already has,
        and tell Python what that encoding is.

        Comment

        • cantabile

          #5
          Re: about coding

          Robert Kern a écrit :
          [color=blue]
          > It depends. Are those characters encoded as UTF-8? Or, more likely, are
          > they encoded as ISO-8859-1?
          >[color=green]
          >> Where can I find a pratical explanation about these encodings ?[/color]
          >
          >
          > http://www.amk.ca/python/howto/unicode
          > http://en.wikipedia.org/wiki/Character_encoding
          > http://www.joelonsoftware.com/articles/Unicode.html
          >[/color]

          I'm using emacs. I guess this is utf-8 encoded...
          Thanks for the links :)

          Comment

          • Paul Watson

            #6
            Re: about coding

            cantabile wrote:[color=blue]
            > Hi, being a newbie in Python, I'm a bit lost with the '-*- coding : -*-'
            > directive.
            >
            > I'm using an accented characters language. Some of them are correctly
            > displayed while one doesn't. I've written :
            > -*- coding: utf-8 -*-
            >
            > Is this wrong ?
            >
            > Where can I find a pratical explanation about these encodings ?
            >
            > Thanks.[/color]

            You must know the encoding produced by your editor.

            If you want to experiment, you could save a small file and run

            od -Ax -tx1 thefile.py

            to see exactly what codepoints appear. Compare those with the
            charts at http://www.unicode.org/ and you can probably surmize
            a good guess.

            Comment

            • John Machin

              #7
              Re: about coding

              Paul Watson wrote:[color=blue]
              > cantabile wrote:
              >[color=green]
              >> Hi, being a newbie in Python, I'm a bit lost with the '-*- coding :
              >> -*-' directive.
              >>
              >> I'm using an accented characters language. Some of them are correctly
              >> displayed while one doesn't. I've written :
              >> -*- coding: utf-8 -*-
              >>
              >> Is this wrong ?
              >>
              >> Where can I find a pratical explanation about these encodings ?
              >>
              >> Thanks.[/color]
              >
              >
              > You must know the encoding produced by your editor.
              >
              > If you want to experiment, you could save a small file and run
              >
              > od -Ax -tx1 thefile.py
              >
              > to see exactly what codepoints appear. Compare those with the
              > charts at http://www.unicode.org/ and you can probably surmize
              > a good guess.[/color]

              Perhaps faster, and certainly available on all platforms (unlike "od"):

              Fire up Idle, then do this:
              [color=blue][color=green][color=darkred]
              >>> import sys
              >>> sys.stdout.enco ding[/color][/color][/color]
              'cp1252'[color=blue][color=green][color=darkred]
              >>>[/color][/color][/color]

              Why Idle, and not from the command line? Well because if you are on
              Windows, you'll get a different answer, which is very unlikely to be the
              one you want [unless you are using a legacy editor].

              C:\junk>python
              Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)] on
              win32
              Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
              >>> import sys
              >>> sys.stdout.enco ding[/color][/color][/color]
              'cp850'[color=blue][color=green][color=darkred]
              >>>[/color][/color][/color]

              Comment

              Working...