Hi All

Please some one Help me find out why this code doesn't work:

codes=['zlib', 'zip', 'base64', 'hex', 'utf-8']

def encoder(str, i):
    return str.encode(codes[i])

def decoder(str, i):
    return str.decode(codes[i])

en = encoder('Some string here', 2)
print en

Recommended Answers

All 6 Replies

It works for me and python 2.6

codes=['zlib', 'zip', 'base64', 'hex', 'utf-8']

def encoder(str, i):
    return str.encode(codes[i])

def decoder(str, i):
    return str.decode(codes[i])

thing = 'Some string here'
for i, k in enumerate(codes):
    en = encoder(thing, i)
    assert thing == decoder(en, i)
    
print "success"

Thanks Gribouillis,
Now it works, but it en/decodes the string in all encodings.
I want to select the encoding freely, for example, something like this:

en1 = encoder('String One', 2)# for base64
en2 = encoder('String Two', 3)# for hex
#en3 = ....
#and so on

Thanks Gribouillis,
Now it works, but it en/decodes the string in all encodings.
I want to select the encoding freely, for example, something like this:

en1 = encoder('String One', 2)# for base64
en2 = encoder('String Two', 3)# for hex
#en3 = ....
#and so on

I don't understand, doesn't it work as expected ? Please describe the expected output of your program and, if it "doesn't work", why it doesn't produce the expected output, or if it raises an exception, which exception ?

Thanks again for your reply

as I said the code works, without exceptions.

it gets the value of "thing" and encodes it to all encodings at once.
its ok, but, I want the User select which encoding he needs the string to be encoded. sorry if i cant be more clear(because of language barrier, Im not an English speaker).

in other word:
i want to input encoding selectively:
Input 1: the string I want to Encode or decode
Input 2: one or two of the encodings in list "codes"

Output: Printing out the Encoded/Decoded String

@pyTony: I want to doing this in my mobile python(compatible with Python 2.2), and it has not all of the libraries

Sorry for my bad English

Thank you both.
problem solved by tweaking the code u gave to me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.