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?
Please help me understand the easiest way to do this..
Collapse
X
-
Tags: None
-
Maybe this will help:[code=Python]Originally posted by Carlo GambinoI 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?
>>> 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] -
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:
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!Code:# >>> letters # 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ'
but when I do the same steps, I get:
Code:>>> letters 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
Comment
-
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
-
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
-
ord()Originally posted by Carlo GambinoI'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!
ie,
[CODE=python]ord('c')
99[/CODE]Comment
Comment