python - 检查字符串是否在数组中

标签 python list unicode

我正在尝试检查一些字符串是否在一个数组中,如下所示:

intact_columns = [...]
for key, value in obj.iteritems():
    if not key in intact_columns:
       print key

问题是:数组中有这样的项目:Reten\xc3\xa7\xc3\xa3o (RET)

我迭代的字符串是这样的:Retenção (RET)

我如何解析数组中的字符串使其看起来像普通字符串?

最佳答案

问题是因为您在两个字符串中使用了不同的编码。我不确定编码类型。将这些解码为 utf-8(或 utf-16)然后检查是安全的。例如:

>>> my_list = ['Reten\xc3\xa7\xc3\xa3o (RET)', 'blah blah ...']
>>> my_string = 'Retenção (RET)'
>>> my_list[0].decode('utf-8')
u'Reten\xe7\xe3o (RET)'
>>> my_string.decode('utf-8')
u'Reten\xe7\xe3o (RET)'

两者都拥有相同的解码值

关于python - 检查字符串是否在数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40409108/

相关文章:

list - 将 C++ 创建的对象追加到 python 列表并使其由 python 管理

pdf - 在 unicode 中使用 xhtml2pdf 时遇到问题

c++ - 使用 boost 和标准 C++ 进行 Unicode 安全查找

python-3.x - Python 请求无法传输 utf-8 编码文件

python - scipy.optimize 加载 DLL 失败?

python - 如何从 scipy.stats 对象获取分布类类型?

python - 是否有可能在 python 中获取临时文件的 md5 哈希?

python - 生成识别给定正则表达式的 DFA 图片

java - 删除索引 N 处的元素,LinkedList

python - 在给定条件的情况下查找列表的最小和最大索引