转换我的列表有我无法在 python 中替换的 unicode 字符串

Posted

技术标签:

【中文标题】转换我的列表有我无法在 python 中替换的 unicode 字符串【英文标题】:convert my list has unicode string which I 'm not be able replace in python 【发布时间】:2015-09-12 04:58:23 【问题描述】:

我有一个这样的列表

[((u'Sylvester Miniter', u'Gillian Miniter'), 110)]  

我需要像在 python 中那样获取它

[(('Sylvester Miniter', 'Gillian Miniter'), 110)]  

有剥离多个操作,但无法做到。请帮忙

【问题讨论】:

你想做什么?要将字符串转换为 ASCII 或其他编码? 【参考方案1】:

使用 str() 将单个字符串转换为普通字符串。

>>> foo = u'Sylvester Miniter'
>>> foo
u'Sylvester Miniter'
>>> str(foo)
'Sylvester Miniter'
>>> 

如果要将unicode字符串转换为ascii

>>> foo.encode('ascii')
'Sylvester Miniter'

Unicode 转 utf-8

>>> foo.encode('utf-8')
'Sylvester Miniter'

【讨论】:

以上是关于转换我的列表有我无法在 python 中替换的 unicode 字符串的主要内容,如果未能解决你的问题,请参考以下文章