c_p*_*ote 705 python string variables types
有没有办法检查python中的变量类型是否为字符串..就像
isinstance(x,int);
Run Code Online (Sandbox Code Playgroud)
对于整数值?
Sve*_*ach 1049
在Python 2.x中,你会这样做
isinstance(s, basestring)
Run Code Online (Sandbox Code Playgroud)
basestring是抽象的超类的str和unicode.它可用于测试对象是否是str或的实例unicode.
在Python 3.x中,正确的测试是
isinstance(s, str)
Run Code Online (Sandbox Code Playgroud)
本bytes类不被认为是在Python 3字符串类型.
And*_*lli 218
我知道这是一个古老的话题,但是作为谷歌上显示的第一个,并且鉴于我没有找到任何令人满意的答案,我将留在这里以供将来参考:
six是Python 2和3兼容库,已经涵盖了这个问题.然后你可以做这样的事情:
import six
if isinstance(value, six.string_types):
pass # It's a string !!
Run Code Online (Sandbox Code Playgroud)
检查代码,这是你发现的:
import sys
PY3 = sys.version_info[0] == 3
if PY3:
string_types = str,
else:
string_types = basestring,
Run Code Online (Sandbox Code Playgroud)
Tex*_*512 55
在Python 3.x或Python 2.7.6中
if type(x) == str:
Run Code Online (Sandbox Code Playgroud)
Gab*_*les 24
type()或isinstance()我不知道为什么在我面前没有一个答案包含这个简单的type(my_variable) is str语法,但type()到目前为止,这样使用对我来说似乎是最合乎逻辑和最简单的:
(在Python3中测试):
# Option 1: check to see if `my_variable` is of type `str`
type(my_variable) is str
# Option 2: check to see if `my_variable` is of type `str`, including
# being a subclass of type `str` (ie: also see if `my_variable` is any object
# which inherits from `str` as a parent class)
isinstance(my_variable, str)
Run Code Online (Sandbox Code Playgroud)
Pythontype()内置函数文档位于: https: //docs.python.org/3/library/functions.html#type。它部分陈述如下。请注意以下注释isinstance():
class type(object)
class type(name, bases, dict, **kwds)使用一个参数,返回对象的类型。返回值是一个类型对象,通常与 所返回的对象相同
object.__class__。建议使用内置函数
isinstance()来测试对象的类型,因为它考虑了子类。
因此,如果您要检查类对象的类型而不是简单变量,并且需要考虑子类,请改为使用isinstance()。请参阅此处的文档: https: //docs.python.org/3/library/functions.html#isinstance。
my_str = "hello"
my_int = 7
print(type(my_str) is str)
print(type(my_int) is str)
print()
print(isinstance(my_str, str))
print(isinstance(my_int, str))
Run Code Online (Sandbox Code Playgroud)
输出:
Run Code Online (Sandbox Code Playgroud)True False True False
F. *_*lor 15
你可以做:
var = 1
if type(var) == int:
print('your variable is an integer')
Run Code Online (Sandbox Code Playgroud)
要么:
var2 = 'this is variable #2'
if type(var2) == str:
print('your variable is a string')
else:
print('your variable IS NOT a string')
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
dic*_*ato 13
如果要检查的不仅仅是整数和字符串,那么类型模块也存在. http://docs.python.org/library/types.html
uml*_*ute 11
因为basestring没有在Python3中定义,这个小技巧可能有助于使代码兼容:
try: # check whether python knows about 'basestring'
basestring
except NameError: # no, it doesn't (it's Python3); use 'str' instead
basestring=str
Run Code Online (Sandbox Code Playgroud)
之后,您可以在Python2和Python3上运行以下测试
isinstance(myvar, basestring)
Run Code Online (Sandbox Code Playgroud)
Wad*_*ler 10
根据以下更好的答案进行编辑.下来大约3个答案,找出basetring的凉爽.
旧答案:注意unicode字符串,您可以从多个地方获取,包括Windows中的所有COM调用.
if isinstance(target, str) or isinstance(target, unicode):
Run Code Online (Sandbox Code Playgroud)
cri*_*aig 10
Python 2/3包括unicode
from __future__ import unicode_literals
from builtins import str # pip install future
isinstance('asdf', str) # True
isinstance(u'asdf', str) # True
Run Code Online (Sandbox Code Playgroud)
http://python-future.org/overview.html
另外我想要注意,如果要检查变量的类型是否是特定类型,可以将变量的类型与已知对象的类型进行比较.
对于字符串,您可以使用它
type(s) == type('')
Run Code Online (Sandbox Code Playgroud)
其他人在这里提供了很多好的建议,但我没有看到一个很好的跨平台总结.对于任何Python程序,以下应该是一个很好的选择:
def isstring(s):
# if we use Python 3
if (sys.version_info[0] >= 3):
return isinstance(s, str)
# we use Python 2
return isinstance(s, basestring)
Run Code Online (Sandbox Code Playgroud)
在这个函数中,我们isinstance(object, classinfo)用来看看我们的输入是str在Python 3中还是basestring在Python 2中.
所以,
您有很多选项可以检查您的变量是否为字符串:
a = "my string"
type(a) == str # first
a.__class__ == str # second
isinstance(a, str) # third
str(a) == a # forth
type(a) == type('') # fifth
Run Code Online (Sandbox Code Playgroud)
此订单是出于有目的.
Python 2的替代方法,不使用basetring:
isinstance(s, (str, unicode))
Run Code Online (Sandbox Code Playgroud)
但是仍然不能在Python 3中工作,因为unicode没有定义(在Python 3中).
a = '1000' # also tested for 'abc100', 'a100bc', '100abc'
isinstance(a, str) or isinstance(a, unicode)
Run Code Online (Sandbox Code Playgroud)
返回True
type(a) in [str, unicode]
Run Code Online (Sandbox Code Playgroud)
返回True
这是我支持 Python 2 和 Python 3 以及这些要求的答案:
six或类似的 compat 模块,因为它们往往会隐藏试图实现的目标。import sys
PY2 = sys.version_info.major == 2
# Check if string (lenient for byte-strings on Py2):
isinstance('abc', basestring if PY2 else str)
# Check if strictly a string (unicode-string):
isinstance('abc', unicode if PY2 else str)
# Check if either string (unicode-string) or byte-string:
isinstance('abc', basestring if PY2 else (str, bytes))
# Check for byte-string (Py3 and Py2.7):
isinstance('abc', bytes)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
803274 次 |
| 最近记录: |