0

guys I got a warning when I run a code I got this warning two times:

UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequallocated at if result2=b and c == 'Pass':

thing is going wired because I use if result2=b and c == 'Pass': three time, only last two have warning. I cannot found some solution on Internet,but they are not work for my code. Here is my code, please help me with that. Thx in advanced!

def XXX1():
    XXXXXX
    if result2==b and c == 'Failed':       ----------no warning
    XXXXXX

def XXX2():
    XXXXXX
    if result2==b and c == 'Failed':       ----------warning
    XXXXXX

def XXX3():
    XXXXXX
    if result2==b and c == 'Pass':         ----------warning
    XXXXXX

some parameters may help:

with open('1.csv','rb') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        if row['Case Id']== 'getpropertyvalue_without_propertyname' :
            a=row['Result Link']
            c=row['Status']


url = a
html = requests.get(url,verify=False).text
soup = BeautifulSoup(html,'html.parser')

result = soup.find("p", {"class":"ERRORLevel"})
result2=result.text
Zub
  • 808
  • 3
  • 12
  • 23
Joe
  • 17
  • 1
  • 4

1 Answers1

3

You are mixing Unicode strings and bytestrings. Python 2 will try to decode the bytestring (as ASCII) when making comparisons, and when that fails you'll get a warning:

>>> u'å', u'å'.encode('utf8')
(u'\xe5', '\xc3\xa5')
>>> 'å' == u'å'
__main__:1: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
False

Don't mix Unicode strings and bytestrings. Decode your text data as early as possible, and only compare Unicode objects. In the above example, the bytestring is UTF-8 encoded, so decoding as UTF-8 first would resolve the warning.

For your example code, BeautifulSoup is (correctly) producing Unicode text. You'll have to decode your CSV data, see Read and Write CSV files including unicode with Python 2.7 for solutions, or decode those two strings manually with str.decode() calls.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343