I'm pulling data from the NHTSA API, using a JSON format. I'm then creating a named tuple from this data and a few other sources and using this as a record to insert into a MySQL database.
The NHTSA API uses ''
to designate a null value which is not an accepted value in for this particular column in database. The column only allows a float
datatype.
When creating my named tuple, is there a way to substitute None
if a specific value is returned? I.e. if API call returns ''
, use None
instead?
Error returned is
Failed inserting object into MySQL table Error while executing statement: Data truncated for column 'weight' at row 1
Tuples are immutable, hence you need to create a new tuple
Here's an example:
old = (1,2,'ABC','','','','text')
new = tuple(None if x == '' else x for x in old)
Output:
Now new
contains:
(1, 2, 'ABC', None, None, None, 'text')
Refer this thread for more information