检查变量类型的最佳方法 [英] Best way to check the type of a variable

查看:36
本文介绍了检查变量类型的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我正在做一些测试以识别列表中元素的类型.

Yesterday I was doing some testing to identify the type of an element from a list.

types={"float":float, "int":int, "str":str}   
try:    
    sql_type = next (k for k,v in types.iteritems() if isinstance (uniqLst[0],v))    
except TypeError as Typeerr:    
    print "Type not right: " + str(Typeerr)    

当然,元素总是一个字符串,因为列表保存的数据来自文本文件.我想知道什么是检查元素真实性质的好方法.您是否真的应该采用 try/except 解决方案,例如:

Well, of course the element turns out to always be a string as the data the list holds derives from a text file. I was wondering what might be a good way to check the true nature of the element. Should you really go for a try/except solution, like:

def check_type(element):
    try:
        int(element)
        return 'int'
    except:
       pass
    try:
       float(element)
       return 'float'
    except:
        pass
    try:
        str(element)
        return 'str'
    except:
        return 'error type'

re.compile 怎么样?(然后像 ('[0-9]+')) 对我来说似乎不太实用.非常感谢任何建议!

What about re.compile? (And then something like ('[0-9]+')) Doesn't seem very practical to me. Any advice is very much appreciated!

干杯,拉尔斯维加斯

推荐答案

根据您对 ikanobori 的回答的评论,您可能正在寻找以下内容 字符串方法:

Based on your comment to ikanobori's answer, you might be looking for the following string methods:

str.isalnum()

如果字符串中的所有字符都是字母数字并且至少有一个字符,则返回 true,否则返回 false.如果以下之一返回 True,则字符 c 是字母数字:c.isalpha()、c.isdecimal()、c.isdigit() 或 c.isnumeric().

Return true if all characters in the string are alphanumeric and there is at least one character, false otherwise. A character c is alphanumeric if one of the following returns True: c.isalpha(), c.isdecimal(), c.isdigit(), or c.isnumeric().

str.isalpha()

如果字符串中的所有字符都是字母并且至少有一个字符,则返回真,否则返回假.字母字符是那些在 Unicode 字符数据库中定义为字母"的字符,即那些具有一般类别属性为Lm"、Lt"、Lu"、Ll"或Lo"之一的字符.请注意,这与 Unicode 标准中定义的字母"属性不同.

Return true if all characters in the string are alphabetic and there is at least one character, false otherwise. Alphabetic characters are those characters defined in the Unicode character database as "Letter", i.e., those with general category property being one of "Lm", "Lt", "Lu", "Ll", or "Lo". Note that this is different from the "Alphabetic" property defined in the Unicode Standard.

str.isdecimal()

如果字符串中的所有字符都是十进制字符并且至少有一个字符,则返回真,否则返回假.十进制字符是来自一般类别Nd"的字符.此类别包括数字字符以及所有可用于形成十进制基数的字符,例如U+0660,阿拉伯-印度数字零.

Return true if all characters in the string are decimal characters and there is at least one character, false otherwise. Decimal characters are those from general category "Nd". This category includes digit characters, and all characters that that can be used to form decimal-radix numbers, e.g. U+0660, ARABIC-INDIC DIGIT ZERO.

str.isdigit()

如果字符串中的所有字符都是数字并且至少有一个字符,则返回真,否则返回假.数字包括十进制字符和需要特殊处理的数字,例如兼容性上标数字.形式上,数字是具有属性值 Numeric_Type=Digit 或 Numeric_Type=Decimal 的字符.

Return true if all characters in the string are digits and there is at least one character, false otherwise. Digits include decimal characters and digits that need special handling, such as the compatibility superscript digits. Formally, a digit is a character that has the property value Numeric_Type=Digit or Numeric_Type=Decimal.

str.isidentifier()

如果根据语言定义、部分标识符和关键字,字符串是有效标识符,则返回 true.

Return true if the string is a valid identifier according to the language definition, section Identifiers and keywords.

str.islower()

如果字符串中的所有大小写字符都是小写并且至少有一个大小写字符,则返回真,否则返回假.大写字符是具有一般类别属性为Lu"、Ll"或Lt"之一的字符,小写字符是具有一般类别属性Ll"的字符.

Return true if all cased characters in the string are lowercase and there is at least one cased character, false otherwise. Cased characters are those with general category property being one of "Lu", "Ll", or "Lt" and lowercase characters are those with general category property "Ll".

str.isnumeric()

如果字符串中的所有字符都是数字字符,并且至少有一个字符,则返回真,否则返回假.数字字符包括数字字符,以及所有具有 Unicode 数值属性的字符,例如U+2155,粗俗分数五分之一.形式上,数字字符是具有属性值 Numeric_Type=Digit、Numeric_Type=Decimal 或 Numeric_Type=Numeric 的字符.

Return true if all characters in the string are numeric characters, and there is at least one character, false otherwise. Numeric characters include digit characters, and all characters that have the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH. Formally, numeric characters are those with the property value Numeric_Type=Digit, Numeric_Type=Decimal or Numeric_Type=Numeric.

str.isprintable()

如果字符串中的所有字符都可打印或字符串为空,则返回真,否则返回假.不可打印字符是在 Unicode 字符数据库中定义为其他"或分隔符"的字符,但被认为可打印的 ASCII 空格 (0x20) 除外.(请注意,此上下文中的可打印字符是在对字符串调用 repr() 时不应转义的字符.它与写入 sys.stdout 或 sys.stderr 的字符串的处理无关.)

Return true if all characters in the string are printable or the string is empty, false otherwise. Nonprintable characters are those characters defined in the Unicode character database as "Other" or "Separator", excepting the ASCII space (0x20) which is considered printable. (Note that printable characters in this context are those which should not be escaped when repr() is invoked on a string. It has no bearing on the handling of strings written to sys.stdout or sys.stderr.)

str.isspace()

如果字符串中只有空白字符且至少有一个字符,则返回真,否则返回假.空白字符是那些在 Unicode 字符数据库中定义为其他"或分隔符"的字符,以及具有双向属性的字符为WS"、B"或S"之一.

Return true if there are only whitespace characters in the string and there is at least one character, false otherwise. Whitespace characters are those characters defined in the Unicode character database as "Other" or "Separator" and those with bidirectional property being one of "WS", "B", or "S".

str.istitle()

如果字符串是标题字符串并且至少有一个字符,则返回true,例如大写字符只能跟在非大小写字符后面,小写字符只能跟在大小写字符后面.否则返回 false.

Return true if the string is a titlecased string and there is at least one character, for example uppercase characters may only follow uncased characters and lowercase characters only cased ones. Return false otherwise.

str.isupper()

如果字符串中的所有大小写字符都是大写并且至少有一个大小写字符,则返回真,否则返回假.大写字符是具有一般类别属性为Lu"、Ll"或Lt"之一的字符,大写字符是具有一般类别属性Lu"的字符.

Return true if all cased characters in the string are uppercase and there is at least one cased character, false otherwise. Cased characters are those with general category property being one of "Lu", "Ll", or "Lt" and uppercase characters are those with general category property "Lu".

这篇关于检查变量类型的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆