How to convert str to python

Asked

Viewed 257 times

2

I have to make a comparison between 2 items, one is in Unicode, and the other is in str, being that what is like str is an array.

for bloqueiosPermanentes in arquivo:
    self.blocks.append(bloqueiosPermanentes.replace("\n", ""))

This is how the append is made in the variable Blocks..

afterward

 for n in j['userList']:
    nicks.append(n['nick'])

this is how the append on the variable nicks is made, it gets its value from a json file pulled from the internet.

Error occurs in the following comparison

for o in nicks:
    if o==self.lblNick.text(): << dá erro aqui na comparacao dos formatos
        pass
    else:
        if o in self.blocks: <<< dá aqui o erro aqui na comparacao dos formatos
            pass

I already tried to use . toUtf8() and Unicode(variable) but none of them had an effect all are not allowed

  • If you could pass a few examples of the entries it would be a good one to test possible solutions.

  • Try to put a print of the error

  • Misses or gives False ?

  • Specify whether you are using Python 2 or Python 3. String and Unicode have many differences between these versions.

1 answer

1

You can convert the value str for unicode thus:

self.blocks.append(unicode(bloqueiosPermanentes.replace("\n", "")))

Or you can make this conversion when comparing:

if o == unicode(self.lblNick.text()):
# ...
if o in map(unicode, self.blocks):

Browser other questions tagged

You are not signed in. Login or sign up in order to post.