Changing numbers into characters using dictionaries

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

    #1

    Changing numbers into characters using dictionaries

    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@7002@707 5@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@7002@707 5@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.
  • Larry Bates

    #2
    Re: Changing numbers into characters using dictionaries



    Danny wrote:[color=blue]
    > 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@7002@707 5@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@7002@707 5@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.[/color]

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

    print ''.join(u)

    Larry Bates

    Comment

    • snoe

      #3
      Re: Changing numbers into characters using dictionaries

      Ok there's a couple things going on here.
      [color=blue]
      > t = text.split('@') // splits the digits/blocks apart from each other[/color]
      this will give you a list:
      ['', '7706', '7002', '7075', '7704']

      You may want to change the line to skip the first empty value:
      t = text.split('@')[1:]

      Next your loop.[color=blue]
      > something = 1
      > while True:
      > var = str(a[t[something]])
      > print var,
      > // I want it to change "@7704@7002@707 5@7704" into "lawl"[/color]

      I think what you want to do here is loop through your list t, take the
      values out of the encrypted text and lookup that value in a.

      In python, you can loop through a list like this:

      for encrypted_char in t:
      var = a[encrypted_char]
      print var,

      now instead of printing each char as you decrypt it you should collect
      them first and do the print at the end. So you get:

      temp_list = []
      for encrypted_char in t:
      var = a[encrypted_char]
      temp_list.appen d(var)
      print ''.join(temp_li st)

      This still won't help with the key error you're getting though, but you
      can catch that error by surrounding your offending line with a
      try/except block:

      temp_list = []
      for encrypted_char in t:
      try:
      var = a[encrypted_char]
      temp_list.appen d(var)
      except KeyError:
      print encrypted_char, "not in", a
      temp_list.appen d('?')
      print ''.join(temp_li st)

      So your final script looks like this:
      text = '@7704@7002@707 5@7704' # some text
      num = '213654' # Number
      s1 = '700'
      s2 = '770'
      s4 = '707' # it adds these later on.
      # splits the digits/blocks apart from each other
      t = text.split('@')[1:]
      a = {s2+num[3]:"l", s1+num[0]:"a", s4+num[5]:"w"}

      temp_list = []
      for encrypted_char in t:
      try:
      var = a[encrypted_char]
      temp_list.appen d(var)
      except KeyError:
      print encrypted_char, "not in", a
      temp_list.appen d('?')
      print ''.join(temp_li st)

      Fixing either your initial text or your a dict depends on your
      requirements.

      Comment

      • Colin Fox

        #4
        Re: Changing numbers into characters using dictionaries

        On Thu, 26 Jan 2006 20:35:26 +0000, Danny wrote:
        [color=blue]
        > 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@7002@707 5@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"}[/color]

        Well, your num[3] is going to return '6', not '4', so your key lookup is
        going to fail right there. Same with num[5], which is 4, not 5.

        --
        Colin Fox
        President
        CF Consulting Inc.

        Comment

        Working...