FXAMN FXAMN - 1 month ago 11
Python Question

Check if string is in array

i'm trying to check if some strings are in one array, like this:

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


The problem is: there are items like this in the array:
Reten\xc3\xa7\xc3\xa3o (RET)


And the strings that i'm iterating over are like this:
Retenção (RET)


How could i parse the strings inside the array to look like normal strings?

Answer

The issue is because you are using the different encoding in both the strings. I am not the sure about the encoding type. It is safe to decode these to utf-8 (or utf-16) and then check. For example:

>>> 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)'

Both holds the same decoded value

Comments