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]
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]
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']