如何检查Python对象是否为字符串(常规或Unicode)?
当前回答
今天晚上,我遇到了一种情况,我认为我必须检查str类型,但事实证明我没有。
我的解决问题的方法可能在许多情况下都有效,所以我在下面提供它,以防其他人对这个问题感兴趣(仅限Python 3)。
# NOTE: fields is an object that COULD be any number of things, including:
# - a single string-like object
# - a string-like object that needs to be converted to a sequence of
# string-like objects at some separator, sep
# - a sequence of string-like objects
def getfields(*fields, sep=' ', validator=lambda f: True):
'''Take a field sequence definition and yield from a validated
field sequence. Accepts a string, a string with separators,
or a sequence of strings'''
if fields:
try:
# single unpack in the case of a single argument
fieldseq, = fields
try:
# convert to string sequence if string
fieldseq = fieldseq.split(sep)
except AttributeError:
# not a string; assume other iterable
pass
except ValueError:
# not a single argument and not a string
fieldseq = fields
invalid_fields = [field for field in fieldseq if not validator(field)]
if invalid_fields:
raise ValueError('One or more field names is invalid:\n'
'{!r}'.format(invalid_fields))
else:
raise ValueError('No fields were provided')
try:
yield from fieldseq
except TypeError as e:
raise ValueError('Single field argument must be a string'
'or an interable') from e
一些测试:
from . import getfields
def test_getfields_novalidation():
result = ['a', 'b']
assert list(getfields('a b')) == result
assert list(getfields('a,b', sep=',')) == result
assert list(getfields('a', 'b')) == result
assert list(getfields(['a', 'b'])) == result
其他回答
Python 2
对于测试对象obj,使用isinstance(obj, basestring)。
文档。
我认为可以安全地假设,如果repr()输出的最后一个字符是'或',那么无论它是什么,它都可以被认为是某种字符串。
def isStr(o):
return repr(o)[-1] in '\'"'
我假设repr不会做任何太繁重的事情,它会返回一个至少有一个字符的字符串。您可以使用类似的方法来支持空字符串
repr(o)[-1:].replace('"', "'") == "'"
但这仍然假设repr返回一个字符串。
Python 2
检查对象o是否是字符串类型的子类的字符串类型:
isinstance(o, basestring)
因为STR和unicode都是basestring的子类。
要检查o的类型是否恰好是str:
type(o) is str
检查o是否是str的实例或str的任何子类:
isinstance(o, str)
如果将str替换为Unicode,上述方法也适用于Unicode字符串。
但是,您可能根本不需要进行显式类型检查。“鸭子打字”可能适合你的需要。见http://docs.python.org/glossary.html # term-duck-typing。
参见python中检查类型的规范方法是什么?
Python 3
在Python 3中。x basestring不再可用,因为str是唯一的字符串类型(与Python 2的语义相同)。x的unicode)。
Python 3中的检查。X就是:
isinstance(obj_to_test, str)
这遵循了官方2to3转换工具的修复:将basestring转换为str。
对于类似字符串的良好鸭子类型方法,它具有与Python 2同时使用的好处。X和3.x:
def is_string(obj):
try:
obj + ''
return True
except TypeError:
return False
Wisefish在切换到isinstance方法之前与duck-typing很接近,只是+=对于列表的含义与+不同。
推荐文章
- 证书验证失败:无法获得本地颁发者证书
- 当使用pip3安装包时,“Python中的ssl模块不可用”
- 无法切换Python与pyenv
- Python if not == vs if !=
- 如何从scikit-learn决策树中提取决策规则?
- 为什么在Mac OS X v10.9 (Mavericks)的终端中apt-get功能不起作用?
- 将旋转的xtick标签与各自的xtick对齐
- Printf与std::字符串?
- 为什么元组可以包含可变项?
- 如何合并字典的字典?
- 如何创建类属性?
- 不区分大小写的“in”
- 在Python中获取迭代器中的元素个数
- 解析日期字符串并更改格式
- 使用try和。Python中的if