3

What is the best way to convert the values in tuples from unicode to string, when the tuples are in a list, can it be done without looping?

unicodedata.normalize('NKFD', x) can only take unicode, not a tuple. The dataset also includes float values.

EXAMPLE

unicode_tuple_list = [(u'text in unicode', u'more unicode'), (u'more text in unicode', u'even more unicode')]

print type(unicode_tuple_list)   # list - keep as list

print type(unicode_tuple_list[0])   # tuple - keep as tuple           

print type(unicode_tuple_list[0][0])   # unicode

How can all these values be made a str?

Community
  • 1
  • 1
cycle_about
  • 325
  • 1
  • 3
  • 6

3 Answers3

5

I'm not sure there is a way to convert this without using a loop/list comprehension.

I would use the map function to accomplish this, see:

unicode_tuple_list = [(u'text in unicode', u'more unicode'), (u'more text in unicode', u'even more unicode')]
string_tuple_list = [tuple(map(str,eachTuple)) for eachTuple in unicode_tuple_list]
print string_tuple_list
Michael Goodwin
  • 700
  • 6
  • 16
  • Thank you Michael, this works great except for a part of the dataset with non-ascii characters. Is there a way to use map to also force encode the tuple values to ascii? – cycle_about Feb 03 '17 at 16:31
  • If you don't need to preserve the special characters you could look at this: http://stackoverflow.com/a/35536228/7108103 if you do need to keep them you can look at this: http://stackoverflow.com/questions/19527279/python-unicode-to-ascii-conversion – Michael Goodwin Feb 03 '17 at 16:41
1

Unpack the tuples, convert to a string and repack.

1
tuple(map(str, unicode_tuple_list))
greenbergé
  • 346
  • 2
  • 9
  • 1
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Donald Duck Mar 10 '17 at 12:24