我想检查一下,如果函数 f1(s)的参数 s 在 MyList 中收到一些字符串(以UTF8格式)然后调用函数 f2 ;但我无法正确比较字符串。
def f1( s ):
MyList = [ u"نامشخص".encode("utf-8") , u"Unknow".encode("utf-8")]
for t in MyList:
if( t == s.encode('utf-8') ):
f2()
return None
break
else:
print "Checked strings: ", t , " =?=" , s.encode("utf-8")
print "Checked strings length: ", len(t), " =?=" , len(s)
return s
检查:
MyList2 = [ u"نامشخص" , "test2".encode("utf-8"), u"نامشخص".encode("utf-8") ]
for a in MyList2:
print "Test String = ", a
f1(a)
print "\n\n"
输出:
Test String = نامشخص
Here[=]
Test String = test2
Checked strings: نامشخص =?= test2
Checked strings length: 12 =?= 5
Checked strings: Unknow =?= test2
Checked strings length: 6 =?= 5
Test String = نامشخص
Traceback (most recent call last):
File "test.py", line 31, in <module>
f1(a)
File "test.py", line 18, in f1
if( t == s.encode('utf-8') ):
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd9 in position 0: ordinal not in range(128)
事实上,我从sqlite db收到字符串 s ,我不知道 s 的编码。 对于来自db f1 错误的某些字符串 s 而言,这是错误的! 似乎 f1 仅适用于某些指定的编码。是否有任何解决方案适用于字符串 s 的所有编码?
答案 0 :(得分:0)
我认为python不喜欢双.encode("utf-8")
。这个脚本也会出现同样的问题:
a = u"نامشخص"
b = a.encode("utf-8") # This work !
c = b.encode("utf-8") # Not that.
您可以使用try / except来处理此问题(请参阅:Test a string if it's Unicode, which UTF standard is and get its length in bytes?)
这可以是一个解决方案:
def get_unicode(s):
try:
return s.encode("utf-8")
except:
return s
编辑:也许是一个更好的测试:
def get_unicode(s):
if isinstance(s, unicode):
return s.encode("utf-8")
return s