Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Easily convert unicode tuple to python string tuple???

Reply
Thread Tools

Re: Easily convert unicode tuple to python string tuple???

 
 
Jeff Epler
Guest
Posts: n/a
 
      04-20-2004
You can use a listcomp to do "something" to each element of an iterable:

>>> s = (u'USER', u'NODE', u'HASH', u'IDNBR')
>>> [i.encode("ascii") for i in s]

['USER', 'NODE', 'HASH', 'IDNBR']

You can use tuple(s) to turn an iterable s into a tuple:
>>> tuple(_)

('USER', 'NODE', 'HASH', 'IDNBR')

Here's a function that does both in one step, as a function:
>>> def decode_tuple(t, encoding="ascii"):

.... return tuple([i.encode(encoding) for i in t])
....
>>> decode_tuple(s)

('USER', 'NODE', 'HASH', 'IDNBR')

Jeff

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple' Jay Parlar Python 0 03-17-2006 02:58 AM
*tuple vs tuple example print os.path.join(os.path.dirname(os.tmpnam()),*("a","b","c")) Steve Python 1 12-13-2005 10:25 PM
Why tuple with one item is no tuple Gregor Horvath Python 37 03-30-2005 06:58 AM
Easily convert unicode tuple to python string tuple??? Michal Mikolajczyk Python 1 04-20-2004 08:37 PM
Re: Easily convert unicode tuple to python string tuple??? Bill Scherer Python 0 04-20-2004 03:34 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57