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 8911761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:01:34+00:00 2026-06-15T04:01:34+00:00

I would like to try to deduce the type of data in a string.

  • 0

I would like to try to deduce the type of data in a string.

Scenario:
I have a CSV file which contains rows of data, and I would like to store this data in a database.
I do not want to store all the fields as strings.
Since the fields in the CSV might change, I cannot assume anything about their types.

Example (CSV file):

[Row 1 - column names] --> "name", "age" , "children"
[Row 2 - data row    ] --> "John", "45.5", "3"
...
[Row n - data row    ] --> ...

In this case, by looking at the data in the rows, I would like to deduce that name is a column of strings, age is a column of floats and children is a column of integers.

My attempt:
The simplest approach would be to try conversions, and decide upon the type when a certain conversion succeeds.
I wrote a method for this purpose which looks like this:

def deduceType(str):
    try:
        #first try to convert to int:
        int(str)
        return 0 #integer
    except ValueError:
        try:
            #not integer, try float:
            float(str)
            return 1 #float
        except ValueError:
            #not float, so deduct string
            return 2 #string

My question:
The problem is that if I want to be able to deduce more data types (booleans, longs, unsigned numeric types, etc…), then this approach becomes cumbersome and inaccurate.

Is there a neater, more efficient and rigorous way to do this?

Answer (edit):
Based on Martijn Pieters answer, I’m doing this:

def deduceType(str):
    try:
        return type(ast.literal_eval(str))
    except ValueError:
        return type('') #string
  • 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-15T04:01:35+00:00Added an answer on June 15, 2026 at 4:01 am

    Use ast.literal_eval() on the value; it’ll interpret it as a python literal. If that fails, you have a string instead.

    >>> import ast
    >>> ast.literal_eval("45.5")
    45.5
    >>> ast.literal_eval("3")
    3
    >>> ast.literal_eval("John")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ast.py", line 68, in literal_eval
        return _convert(node_or_string)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ast.py", line 67, in _convert
        raise ValueError('malformed string')
    ValueError: malformed string
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an OS design project with some subprojects, I would like to try
I would like to try out the Google App Engine Python environment, which the
I would like to try open file without extension. When I try to open
I would like to try TWO different things (both have a strong possibility of
I would like to try C memcpy function. I have this code: char destination[40];
I have some animation I would like to try, kind of a shopping cart/checkout
I have an ASP.NET application which I'd like to try running on Mono, just
I would like to try to parse a string as a DateTime? , and
I have a few ideas I would like to try out in the Disk
I have legacy database and tables that I would like to try to import

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.