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

Discussion in 'Python' started by Jeff Epler, Apr 20, 2004.

  1. Jeff Epler

    Jeff Epler Guest

    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
    Jeff Epler, Apr 20, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michal Mikolajczyk
    Replies:
    1
    Views:
    809
    Larry Bates
    Apr 20, 2004
  2. Bill Scherer
    Replies:
    0
    Views:
    613
    Bill Scherer
    Apr 20, 2004
  3. Chris  Song
    Replies:
    3
    Views:
    271
    Ganesan Rajagopal
    Dec 27, 2005
  4. Jeremy
    Replies:
    1
    Views:
    805
    Alex Willmer
    Jan 11, 2011
  5. Jeremy
    Replies:
    0
    Views:
    577
    Jeremy
    Jan 11, 2011
Loading...

Share This Page