473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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@7075@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@7075@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.
Jan 26 '06 #1
3 1113


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@7002@7075@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@7075@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@7002@7075@7704'
a={'7704':'l','7002':'a','7075':'w'}
u=[]
for c in text.split('@')[1:]:
u.append(a[c])

print ''.join(u)

Larry Bates
Jan 26 '06 #2
Ok there's a couple things going on here.
t = text.split('@') // splits the digits/blocks apart from each other 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. something = 1
while True:
var = str(a[t[something]])
print var,
// I want it to change "@7704@7002@7075@7704" into "lawl"


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.append(var)
print ''.join(temp_list)

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.append(var)
except KeyError:
print encrypted_char, "not in", a
temp_list.append('?')
print ''.join(temp_list)

So your final script looks like this:
text = '@7704@7002@7075@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.append(var)
except KeyError:
print encrypted_char, "not in", a
temp_list.append('?')
print ''.join(temp_list)

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

Jan 26 '06 #3
On Thu, 26 Jan 2006 20:35:26 +0000, 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@7002@7075@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"}


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.

Jan 27 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Eli Daniel | last post by:
Hi, I'm new to Python. Can you please tell me if the following is possible. My users are using other scripting launguage to write scripts. They are used to write somthing like (keeping it...
31
by: Bo Peng | last post by:
Dear list, I have many dictionaries with the same set of keys and I would like to write a function to calculate something based on these values. For example, I have a = {'x':1, 'y':2} b =...
7
by: klaus hoffmann | last post by:
Is it possible to convert 2 characters from a stringstream to an integer without using an intermediate a 2-bytes string ? The following fragment doesn't work #include <iostream> #include...
9
by: bissatch | last post by:
Hi, Is it possible to change the class style of an HTML element using DHTML? For example... <td class="my_class">Text</td> I have used DHTML to change style elements such as backgroundColor...
2
by: Paulo Rodrigues | last post by:
Hi I would like some help about the following : I have a text field and I don't want it contains any numbers. How can I limit this situation ? So far, I couldn't find literature exacly...
16
by: StenKoll | last post by:
Help needed in order to create a register of stocks in a company. In accordance with local laws I need to give each individual share a number. I have accomplished this by establishing three tables...
4
by: Pokerkook | last post by:
Hello, If anybody could help me with this I would greatly appreciate it. Or at least tell me why I get the output of this garbage: 49 49 10 49 52
14
by: Vlad | last post by:
Please consider this code public class MyClass{ public bool MyMethod1(){ return false; } public bool MyMethod2(){ int x=0,y=1/x; return false; }
7
by: teh.sn1tch | last post by:
I created a random number generator for an application that uses a MersenneTwister class I found on the net. Basically I generate two random numbers from the MersenneTwister class and use each one...
3
by: ayman723 | last post by:
hi I had this problem where I was asked to : Write a program that compares and records the time taken to sort an array of social security numbers using four sorting algorithms. The program...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.