Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python convert tuple values from unicode to str

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?

like image 887
cycle_about Avatar asked Sep 01 '26 02:09

cycle_about


1 Answers

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
like image 70
Michael Goodwin Avatar answered Sep 03 '26 18:09

Michael Goodwin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!