determine “type of value” from a string in python

后端 未结 4 635
难免孤独
难免孤独 2021-01-14 02:39

I\'m trying to write a function in python, which will determine what type of value is in string; for example

if in string is 1 or 0 or True or False the value is BI

4条回答
  •  有刺的猬
    2021-01-14 03:11

    In reply to

    For example it doesn't work for 2010-00-10 which should be Text, but is INT or 20.90, which should be float but is int

    >>> import re
    >>> patternINT=re.compile('[0-9]+')
    >>> print patternINT.match('2010-00-10')
    <_sre.SRE_Match object at 0x7fa17bc69850>
    >>> patternINT=re.compile('[0-9]+$')
    >>> print patternINT.match('2010-00-10')
    None
    >>> print patternINT.match('2010')
    <_sre.SRE_Match object at 0x7fa17bc69850>
    

    Don't forget $ to limit ending of string.

提交回复
热议问题