我正在尝试比较两个字符串last_content
和new_content
。正在从文件中读取一个字符串,另一个从BeautifulSoup传递。当我尝试比较两者时,我收到以下错误:
UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
这就是我填充last_content
和new_content
的方式:
f = open('content.txt', 'r')
last_content = str(f.read())
new_content = soup.find_all('div',{'class': 'threadtitleline'})[2]
这是抛出错误的条件:
if new_content == last_content:
do something awesome
else:
do something even more awesome
我有没有办法对这些字符串进行编码以避免此错误?
答案 0 :(得分:1)
with io.open('content.txt', 'r', encoding='utf-8') as f:
last_content = f.read()
写出来时不要忘记这样做。