Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7816045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:43:32+00:00 2026-06-02T05:43:32+00:00

I’m trying to write a function in python, which will determine what type of

  • 0

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 BIT

if in string is 0-9*, the value is INT

if in string is 0-9+.0-9+ the value is float

if in string is stg more (text, etc) value is text

so far i have stg like

def dataType(string):

 odp=''
 patternBIT=re.compile('[01]')
 patternINT=re.compile('[0-9]+')
 patternFLOAT=re.compile('[0-9]+\.[0-9]+')
 patternTEXT=re.compile('[a-zA-Z0-9]+')
 if patternTEXT.match(string):
     odp= "text"
 if patternFLOAT.match(string):
     odp= "FLOAT"
 if patternINT.match(string):
     odp= "INT"
 if patternBIT.match(string):
     odp= "BIT"

 return odp 

But i’m not very skilled in using regexes in python..could you please tell, what am i doing wrong? 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T05:43:34+00:00Added an answer on June 2, 2026 at 5:43 am

    Before you go too far down the regex route, have you considered using ast.literal_eval

    Examples:

    In [35]: ast.literal_eval('1')
    Out[35]: 1
    
    In [36]: type(ast.literal_eval('1'))
    Out[36]: int
    
    In [38]: type(ast.literal_eval('1.0'))
    Out[38]: float
    
    In [40]: type(ast.literal_eval('[1,2,3]'))
    Out[40]: list
    

    May as well use Python to parse it for you!

    OK, here is a bigger example:

    import ast, re
    def dataType(str):
        str=str.strip()
        if len(str) == 0: return 'BLANK'
        try:
            t=ast.literal_eval(str)
    
        except ValueError:
            return 'TEXT'
        except SyntaxError:
            return 'TEXT'
    
        else:
            if type(t) in [int, long, float, bool]:
                if t in set((True,False)):
                    return 'BIT'
                if type(t) is int or type(t) is long:
                    return 'INT'
                if type(t) is float:
                    return 'FLOAT'
            else:
                return 'TEXT' 
    
    
    
    testSet=['   1  ', ' 0 ', 'True', 'False',   #should all be BIT
             '12', '34l', '-3','03',              #should all be INT
             '1.2', '-20.4', '1e66', '35.','-   .2','-.2e6',      #should all be FLOAT
             '10-1', 'def', '10,2', '[1,2]','35.9.6','35..','.']
    
    for t in testSet:
        print "{:10}:{}".format(t,dataType(t))
    

    Output:

       1      :BIT
     0        :BIT
    True      :BIT
    False     :BIT
    12        :INT
    34l       :INT
    -3        :INT
    03        :INT
    1.2       :FLOAT
    -20.4     :FLOAT
    1e66      :FLOAT
    35.       :FLOAT
    -   .2    :FLOAT
    -.2e6     :FLOAT
    10-1      :TEXT
    def       :TEXT
    10,2      :TEXT
    [1,2]     :TEXT
    35.9.6    :TEXT
    35..      :TEXT
    .         :TEXT
    

    And if you positively MUST have a regex solution, which produces the same results, here it is:

    def regDataType(str):
        str=str.strip()
        if len(str) == 0: return 'BLANK'
    
        if re.match(r'True$|^False$|^0$|^1$', str):
            return 'BIT'
        if re.match(r'([-+]\s*)?\d+[lL]?$', str): 
            return 'INT'
        if re.match(r'([-+]\s*)?[1-9][0-9]*\.?[0-9]*([Ee][+-]?[0-9]+)?$', str): 
            return 'FLOAT'
        if re.match(r'([-+]\s*)?[0-9]*\.?[0-9][0-9]*([Ee][+-]?[0-9]+)?$', str): 
            return 'FLOAT'
    
        return 'TEXT' 
    

    I cannot recommend the regex over the ast version however; just let Python do the interpretation of what it thinks these data types are rather than interpret them with a regex…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
i got an object with contents of html markup in it, for example: string
I need a function that will clean a strings' special characters. I do NOT
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.