Plone.api.create empty number values

I have a list of content which I want to import to Plone with plone.api.create.
All fields are originally strings, so I have to convert some of them to ints and floats. The fields are not required, so this will give me a problem with 'empty fields' when creating the content ( since int("") and float("") gives errors)

IS there a way to do this without a lot of checking for 'isvalue' or similar (there are much more fields than in the example below:

new_book= api.content.create(
        type='book',
            id=i['id'],
            title=i['title']
            ...
            
            pages = int(i['sideantall']),
            weight = float(i['weight']),
            pricegross = float(i['priceGross'])

            ... etc

        )
int(i['weight'].strip() or 0)

Thanks
PS: I needed it empty if not defined, so probably:

int(i['weight'].strip() or None)

>>> int(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

A better approach could be to "clean" the data before, eg:

i['weight'] = None if i['weight'].strip() == '' else int(i['weight'].strip())

Thanks.

I (just) tried with my syntax, and it did not work.
PS; I am not sure if the last strip() is needed, since int("10") gives save result as int(" 10 " )

weight =  None if i['weight'].strip() == '' else float(i['weight']),
1 Like

Plone Foundation Code of Conduct