Please help me understand the easiest way to do this..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Carlo Gambino
    New Member
    • Feb 2008
    • 12

    Please help me understand the easiest way to do this..

    I want to assign each ASCII character the associated decimal value. Can anyone point me in the right direction to do this without manually defining each character?
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by Carlo Gambino
    I want to assign each ASCII character the associated decimal value. Can anyone point me in the right direction to do this without manually defining each character?
    Maybe this will help:[code=Python]
    >>> import string
    >>> letters = string.letters
    >>> letters
    'abcdefghijklmn opqrstuvwxyzABC DEFGHIJKLMNOPQR STUVWXYZ'
    >>> ord('a')
    97
    >>> dd = dict(zip(letter s, range(ord('a'), ord('z')+1)+ran ge(ord('A'), ord('Z')+1)))
    >>> globals().updat e(dd)
    >>> A
    65
    >>> B
    66
    >>> C
    67
    >>> [/code]

    Comment

    • Carlo Gambino
      New Member
      • Feb 2008
      • 12

      #3
      dd = dict(zip(letter s, range(ord('a'), ord('z')+1)+ran ge(ord('A'), ord('Z')+1)))

      Well this is certainly pointing me in the right direction (I think). Thanks for the post!

      It does bring me to a question, however. In your example, your output for A is 66. When I replicate the code, I get an output of 97. If I pay more attention, I find your code:

      Code:
      #
      >>> letters
      #
      'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW  XYZ'
      one would think such a think is standard. I'll try this code and we'll see where it ends up. Thanks, again, for the post!

      but when I do the same steps, I get:

      Code:
      >>> letters
      'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Maybe the following will solve that possible problem:[code=Python]import string

        letters = string.lowercas e+string.upperc ase

        dd = dict(zip(letter s, range(ord(strin g.lowercase[0]), \
        ord(string.lowe rcase[-1])+1)+\
        range(ord(strin g.uppercase[0]), \
        ord(string.uppe rcase[-1])+1))
        )

        globals().updat e(dd)
        [/code]
        >>> A
        65
        >>> a
        97
        >>> z
        122
        >>> Z
        90
        >>>

        Comment

        • Carlo Gambino
          New Member
          • Feb 2008
          • 12

          #5
          I'm not sure I'm able to understand how to apply the code above to the purpose at hand. Is there any way to convert the character to it's decimal value? I've seen this done with C/C++, C#, even VB, I have a hard time believing python can't!

          Comment

          • jlm699
            Contributor
            • Jul 2007
            • 314

            #6
            Originally posted by Carlo Gambino
            I'm not sure I'm able to understand how to apply the code above to the purpose at hand. Is there any way to convert the character to it's decimal value? I've seen this done with C/C++, C#, even VB, I have a hard time believing python can't!
            ord()
            ie,
            [CODE=python]ord('c')
            99[/CODE]

            Comment

            Working...