簡體   English   中英

Python將元組值從unicode轉換為str

[英]Python convert tuple values from unicode to str

將元組中的值從unicode轉換為字符串的最佳方法是什么,當元組在列表中時,是否可以在不循環的情況下完成?

unicodedata.normalize('NKFD', x)只能采用unicode,而不是元組。 數據集還包括浮點值。

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

如何才能使所有這些價值成為一個str

我不確定有沒有辦法在不使用循環/列表理解的情況下轉換它。

我會用map函數來完成這個,參見:

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

解壓縮元組,轉換為字符串並重新打包。

tuple(map(str, unicode_tuple_list))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM