type unicode and type str



用python 2.6.7编写的程序!

if type(value) == str and 'count(*)' in value:
   testcase['isCountQuery'] = 'true'
   break

我的测试用例不会通过,因为valueunicode 类型

(Pdb) type(value) == str
False
(Pdb) value
u'select count(*) from uc_etl.agency_1'
(Pdb) type(value)
<type 'unicode'>
(Pdb) value
u'select count(*) from uc_etl.agency_1'

我尝试将if语句更改为:

if type(value) == unicode and 'count(*)' in value:
    testcase['isCountQuery'] = 'true'
    break

type==unicode不存在。

我可以包装str(value),但我想知道这个是否还有其他修复方法

我该怎么解决这个问题?

至少在python 2中,您可以检查值是否为basestring(str和unicode的基类)。

if isinstance(value, basestring)  and 'count(*)' in value:
   testcase['isCountQuery'] = 'true'
   break

[Python 2.Docs]:内置函数-isistance(object,classinfo)是首选方式:

from types import StringType, UnicodeType
if value and isinstance(value, (StringType, UnicodeType)) and "count(*)" in value:
    #the rest of the code.

不确定为什么没有定义unicode

最新更新