0

I'm get stuck triyng to transform only one word from unicode into a plain string. I look for answers but no one help me to solve this simple problem.

I'v already tried the following links: https://www.oreilly.com/library/view/python-cookbook/0596001673/ch03s18.html

Convert a Unicode string to a string in Python (containing extra symbols)

How to convert unicode string into normal text in python

from bs4 import BeautifulSoup

r = requests.get('https://www.mpgo.mp.br/coliseu/concursos/inscricoes_abertas')
soup = BeautifulSoup(r.content, 'html.parser')

table = soup.find('table', attrs={'class':'grid'})

text = table.get_text()
text_str = text[0:7]
text_str = text_str.encode('utf-8')

test_str = 'Nenhum'
test_str = test_str.encode('utf-8')

if text_str == test_str:
    print('Ok they are equal')
else:
    print(id(text_str))
    print(id(test_str))
    print(type(test_str))
    print(type(test_str))
    print(test_str)
    print(test_str)```

My spected result is: text_str being equal test_str
ZF007
  • 3,708
  • 8
  • 29
  • 48

1 Answers1

0

Welcome to SO. You have a typo in your debug output. The last 4 values are all test_str instead of some text_str.

Then you would have noticed that your read in variable contains:

'\nNenhum'

So if you either change your slice to: text_str = text[1:7] or if you set your test string accordingly:

test_str = '\nNenhum'

It works. Happy hacking...

HeyMan
  • 1,529
  • 18
  • 32