[Tutor] Python script errors

Peter Otten __peter__ at web.de
Wed Dec 12 12:12:51 EST 2018


Ravi Kumar wrote:

> Hi,
> 
> I have developed a python script to get api calls for meraki
> clientlogevents Thanks for all the help previously  I am facing few errors
> such as
> 
> Json_string=r.json()
> 
> raw_decode
>     raise JSONDecodeError("Expecting value", s, err.value) from None
> json.decoder.JSONDecodeError: Expecting value: line 1 column 2 (char 1)
>>>>
> 
> '{0:20}   {1:30}   {2:16}   {3:18}   {4:10}     {5:11} '.format(
> deviceserial, type, macaddress,occurredAt, details))
> TypeError: unsupported format string passed to NoneType.__format__
> 
> I know this occurs when the api response is nulls but how do I handle
> this?

Say x can be a string or None. 

>>> for x in ["a string", None]:
...     "{:20}".format(x)
... 
'a string            '
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: unsupported format string passed to NoneType.__format__

The string can be padded while None does not support that. The best solution 
is usually to replace None with a string that cannot occur normally, and 
then to use that instead of None. Example:

>>> for x in ["a string", None]:
...     "{:20}".format("#NONE" if x is None else x)
... 
'a string            '
'#NONE               '

Slightly more convenient: you can force string conversion before padding:

>>> for x in ["a string", None]:
...     "{!s:20}".format(x)
... 
'a string            '
'None                '






More information about the Tutor mailing list