Python 2.X 下,比较两个字符串是否相同
2015-05-18 22:43
465 查看
在实际操作中,将一个dict数据类型中的值,与从Excel表中提取的一系列数据进行比较,看dict中的数据是否与Excel匹配。可能会遇到如下问题:
UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
出现这个错误后,字符串无法正确匹配,便会出现找不到匹配项的结果。
部分相关代码为:
<p><span style="font-size:18px;">import xlrd</span></p><p></p><p style="margin-top: 0px; margin-bottom: 0px;"><span style="font-size:18px;">bk=xlrd.open_workbook(str_file)</span></p><p style="margin-top: 0px; margin-bottom: 0px;"><span style="font-size:18px;">sh=bk.sheet_by_index(0)</span></p><p style="margin-top: 0px; margin-bottom: 0px;"><span style="font-size:18px;">n=sh.nrows</span></p>
<span style="font-size:18px;">flag=0
for i in range(n):
str1=sh.row(i)[0].value
str2=sh.row(i)[1].value
if(dict1['no']==str1):
if(dict1['name']==str2):
print "Your input 'no':%s,'name':'%s' is right." %(dict1['no'],dict1['name'])
flag=1</span>
经过查找原因,发现是字符串类型不匹配。
type(dict1['name']是str,而type(str1)是unicode。
解决办法为:
将
<span style="font-size:18px;"> if(dict1['no']==str1):
if(dict1['name']==str2):</span>
改为
<span style="font-size:18px;"> if(dict1['no']==str1.encode('utf8')):
if(dict1['name']==str2.encode('utf8')):</span>即可解决字符串转换问题。将unicode码转换成utf8。
该解决方法在本人实际操作中验证可行。仅供参考。
UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
出现这个错误后,字符串无法正确匹配,便会出现找不到匹配项的结果。
部分相关代码为:
<p><span style="font-size:18px;">import xlrd</span></p><p></p><p style="margin-top: 0px; margin-bottom: 0px;"><span style="font-size:18px;">bk=xlrd.open_workbook(str_file)</span></p><p style="margin-top: 0px; margin-bottom: 0px;"><span style="font-size:18px;">sh=bk.sheet_by_index(0)</span></p><p style="margin-top: 0px; margin-bottom: 0px;"><span style="font-size:18px;">n=sh.nrows</span></p>
<span style="font-size:18px;">flag=0
for i in range(n):
str1=sh.row(i)[0].value
str2=sh.row(i)[1].value
if(dict1['no']==str1):
if(dict1['name']==str2):
print "Your input 'no':%s,'name':'%s' is right." %(dict1['no'],dict1['name'])
flag=1</span>
<span style="font-size:18px;">if(flag==0): print "Your input 'no':%s is not exist." % dict1['no']</span>
经过查找原因,发现是字符串类型不匹配。
type(dict1['name']是str,而type(str1)是unicode。
解决办法为:
将
<span style="font-size:18px;"> if(dict1['no']==str1):
if(dict1['name']==str2):</span>
改为
<span style="font-size:18px;"> if(dict1['no']==str1.encode('utf8')):
if(dict1['name']==str2.encode('utf8')):</span>即可解决字符串转换问题。将unicode码转换成utf8。
该解决方法在本人实际操作中验证可行。仅供参考。
相关文章推荐
- 使用python语言,比较两个字符串是否相同
- 使用python语言,比较两个字符串是否相同的实例
- Python3 比较两个图片是否类似或相同
- php比较两个字符串是否相同
- 汇编语言: 试编写一段程序,要求比较两个字符串 string1 和 string2 所含字符是否相等,如相 等则显示“MATCH”, 若不相同则显示“NO MATCH”。
- php比较两个字符串是否相同
- python中比较两个文件是否相同
- C#中使用Equals()函数比较两个字符串是否相同
- 6-2-1 字符串-字符串变量String-创建字符串变量-初始化字符串变量-字符串连接-输入字符串(单词、行)-对象变量的赋值-比较两个字符串是否同一个-比较两个字符串内容是否相同
- 翻转字符串和比较两个字符串是否相同
- asp.net下比较两个等长字符串是否含有完全相同字符(忽略字符顺序)
- 两个字符串比较是否相同
- Valid Anagram 比较两个字符串是否相同
- matlab之比较两个字符串前几位是否相同
- 翻转字符串和比较两个字符串是否相同
- leetcode_318. Maximum Product of Word Lengths 求两个不相交的字符串的长度乘积的最大值,将字母转换成二进制形式,按位与比较是否有相同字母
- 翻转字符串和比较两个字符串是否相同
- 比较两个等长字符串是否含有完全相同字符(忽略字符顺序)
- 翻转字符串和比较两个字符串是否相同