1

I'm having lists as shown below.

l = ['22','abc','znijh09nmm','928.2','-98','2018-01-02']

I want those to insert in MySQL using Python, but I want it to get the output as:

l = [22,'abc','znijh09nmm',928.2,-98,2018-01-02]
Matze
  • 5,100
  • 6
  • 46
  • 69
  • 4
    The date value should probably stay a string? How does your python code to insert into your sql look like? – Patrick Artner Aug 28 '18 at 09:48
  • Your use of _insert_ and _output_ confuses me. Do you want to put the data into your mysql database? Do you want to print/output the data to console? What is the exact problem you are facing in your code (error message/stack trace)? – Patrick Artner Aug 28 '18 at 09:52

1 Answers1

0

You could use something like this to convert integers and floats in the list.

mylist = ['22','abc','znijh09nmm','928.2','-98','2018-01-02']

def convert(value):
    if str(value).isdigit():
        return int(value)
    else:
        try:
            float(value)
            return float(value) 
        except ValueError:
            return value

mylist = [convert(i) for i in mylist]
print(mylist)

[22, 'abc', 'znijh09nmm', 928.2, -98.0, '2018-01-02']
Abhi
  • 4,068
  • 1
  • 16
  • 29