我无法将Unicode转换为纯字符串

问题描述 投票:0回答:1

我被困在triyng中,只能将一个单词从unicode转换成纯字符串。我在寻找答案,但没有人帮助我解决这个简单的问题。

我已经尝试了以下链接: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
python-3.x beautifulsoup
1个回答
0
投票

欢迎您加入。您的调试输出中有错字。最后4个值均为te s t_str,而不是某些te x t_str。

然后您会注意到您的读入变量包含:

'\nNenhum'

因此,如果您将切片更改为:text_str = text [1:7]或相应地设置了测试字符串:

test_str = '\nNenhum'

有效。骇客入侵...

© www.soinside.com 2019 - 2024. All rights reserved.