比较两个字符串时出现Unicode错误

时间:2014-08-26 21:01:26

标签: python-2.7 beautifulsoup

我正在尝试比较两个字符串last_contentnew_content。正在从文件中读取一个字符串,另一个从BeautifulSoup传递。当我尝试比较两者时,我收到以下错误:

UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal

这就是我填充last_contentnew_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

我有没有办法对这些字符串进行编码以避免此错误?

1 个答案:

答案 0 :(得分:1)

with io.open('content.txt', 'r', encoding='utf-8') as f:
  last_content = f.read()

写出来时不要忘记这样做。

Unicode in Python, Completely Demystified