Changing numbers into characters using dictionaries

Larry Bates larry.bates at websafe.com
Thu Jan 26 22:08:11 CET 2006



Danny wrote:
> Hello again,
> 
> I am now trying to make something to change some "encrypted" text into
> some plain text, here is the code I have so far:
> 
> text = '@7704 at 7002@7075 at 7704' // some text
> num = '213654' // Number
> s1 = '700'
> s2 = '770'
> s4 = '707' // it adds these later on.
> t = text.split('@') // splits the digits/blocks apart from each other
> a = {s2+num[3]:"l", s1+num[0]:"a", s4+num[5]:"w"}
> 
> something = 1
> while True:
>     var = str(a[t[something]])
>     print var,
> // I want it to change "@7704 at 7002@7075 at 7704" into "lawl"
> 
> I get the error:
> Traceback (most recent call last):
>   File "C:/Documents and Settings/Danny/My
> Documents/python/changetext.py", line 9, in ?
>     var = str(a[t[something]])
> KeyError: '7704'
> 
> I've explained what is needed to happen in the comments. Also, if any of
> you can think of a better way to do this can you possibly tell me this?
> Thanks.

text = '@7704 at 7002@7075 at 7704'
a={'7704':'l','7002':'a','7075':'w'}
u=[]
for c in text.split('@')[1:]:
    u.append(a[c])

print ''.join(u)

Larry Bates



More information about the Python-list mailing list