Special characters in text...


Results 1 to 7 of 7

Thread: Special characters in text...

  1. #1
    Join Date
    Mar 2004
    Location
    Harford County, Maryland
    Posts
    135

    Special characters in text...(Python)

    I am attempting to format [circular] degrees into a "pretty print" format, e.g.:

    243[deg] 43' 17"

    in a [Python] script that I am writing, scrunching some land-surveying data.

    What I want to do is place the degree symbol (the little super case 'o') where I have [deg] in this example.

    I've searched thru the modules without reward (not to say that I've looked everywhere that exists, but that after combing thru all the 'usual suspects') I find nothing helpful.

    The python project can't figure out what I'm trying to ask to do.

    Using digraphs in my editor (Vim) is of no value. They don't translate thru the compiler without complaining.

    SOMEBODY out there has tackled this issue. Gimme a hint...
    Last edited by chzlchp; 11-06-2005 at 02:14 PM.
    Success isn't measured by how high you fly: Success is measured by how high you bounce!

  2. #2
    Join Date
    Apr 2001
    Location
    SF Bay Area, CA
    Posts
    14,936
    Make it a Unicode string, and put the named code point "DEGREE SIGN" in your string:

    str = u"\N{DEGREE SIGN}"

    then print str out. As long as you're running on a terminal that supports UTF-8, I'm fairly sure it'll work.

    Or, see:

    http://mail.python.org/pipermail/pyt...ne/046945.html

    (fourth hit from a Google search for "unicode degree symbol"); the person there has explained how to encode the degree symbol in several different encodings. (But if your terminal supports UTF-8 natively, I'm fairly sure you can just "print str" and it'll work.)

  3. #3
    Join Date
    Mar 2004
    Location
    Harford County, Maryland
    Posts
    135

    well, I'll be darned...

    Your primary suggestion worked right out of the box, e.g.

    print "243" + u"\N{DEGREE SIGN}"

    created exactly the formatting I wanted.

    I'm not sure what the lesson learned here is[?!] I had used as the lynchpin for my search the notation UTF-8, and I know I ran across some references to unicode, but the significance alluded me. I tried (after) reading your comments to go thru 'man unicode', but it didn't seem to make sense, or relate to my question.

    Been an interesting day, though. I did stumble across two or 3 things I didn't know I didn't know, so the Rute User's advice to obtain knowledge thru benign assimilation has much to recommend it.

    Anyway, I now have 2 or three brand new areas to explore and get thoroughly confused in. Nothing like a fresh start. Thanks a whole lot for your help!
    Success isn't measured by how high you fly: Success is measured by how high you bounce!

  4. #4
    Join Date
    Mar 2003
    Location
    Tampa, FL USA
    Posts
    2,193
    Another suggestion, though this isn't unicode safe.

    Try:
    Code:
    for i in xrange(256):
            print str(i) + " = " + chr(i)
    Run that and look for which one is the "degrees" symbol. (I couldn't find it in my character set.)

    "chr(number)" turns a number between 0 and 255 to a character. "ord(character)" does the opposite.

  5. #5
    Join Date
    Apr 2001
    Location
    SF Bay Area, CA
    Posts
    14,936
    Quote Originally Posted by chzlchp
    I had used as the lynchpin for my search the notation UTF-8, and I know I ran across some references to unicode, but the significance alluded me.
    Well, OK, let me go through some stuff that I think is fairly basic.

    Unicode (AKA ISO-10646) is a system where "code points" (numeric values) are mapped to characters with names. Every character has a Unicode name -- DEGREE SYMBOL, or LATIN CAPITAL LETTER A, or LATIN LOWERCASE LETTER Q, etc., etc.

    Unicode does not specify how to store the various code points in memory or on disk, though. That's up to the encoding. UTF-8 is one possible encoding (as is UCS-2, which Microsoft calls Unicode in all its documentation, where most code points are stored as 2 bytes (but some code points must be escaped), UTF-7, which yields byte values that are legal to pass between systems that only understand 7-bit characters, UCS-4, where every code point is 4 bytes (no escaping at all is required in UCS-4), and several others). In UTF-8, the first 127 characters map exactly onto the old 7-bit ASCII character set, so UTF-8 is often used where backwards compatibility with 7-bit ASCII is important.

    I believe that Unicode strings in Python (u"whatever") get stored as either UTF-16 or UTF-32, but I'm not quite sure which. They get displayed (and passed to other programs) as UTF-8. So your terminal has to be able to handle UTF-8 characters (as do any programs you pass Unicode strings to through the various popen calls), otherwise you'll see strangeness.

    I'm also not sure how much help the "unicode" manpage really is -- it's fairly technical, and reads a lot like a standard. It doesn't really help when you're looking for overview info (not that I necessarily helped much either, come to think of it).
    Last edited by bwkaz; 11-07-2005 at 08:04 PM.

  6. #6
    Join Date
    Mar 2004
    Location
    Harford County, Maryland
    Posts
    135

    Not gonna be a quiz on this, is there?

    [QUOTE=bwkaz]Well, OK, let me go through some stuff that I think is fairly basic.

    Basic?! Whew!

    What I'm gonna do is make a hard-copy of your comments (thanks!) and put them in my ever-increasing 3-ring binder...there for when I think it will all make some sense to me.

    I just started a [python] script wherein I was able to correctly format an example in my commenting, and thanks to the help, I'll be able to format the output of the script. Couldn't do any of that 24 hours ago.

    One little step at a time...I'm gonna beat this stuff, and somewhere down the line actually learn something more about Linux than just how to manage my [Vim/Python] script files.*

    Appreciate the help.

    *(Why not just do it all in Windows, where I have a nice Python interpreter, scripts will run on a mouse click, and I can print out coding with the syntax coloring? I really dunno. I'm taking it on blind faith that this-Linux-is the right place to be. I still really don't know why. Can't even get my printer or scanner to work for me.)
    Success isn't measured by how high you fly: Success is measured by how high you bounce!

  7. #7
    Join Date
    Jan 2003
    Posts
    979
    scripts will run on a mouse click
    doesn't it?

    I can print out coding with the syntax coloring
    whats wrong with the syntax coloring of linux?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •