• Printing dict value for possibly undefined key

    From Loris Bennett@21:1/5 to All on Fri Nov 24 15:31:20 2023
    Hi,

    I want to print some records from a database table where one of the
    fields contains a JSON string which is read into a dict. I am doing
    something like

    print(f"{id} {d['foo']} {d['bar']}")

    However, the dict does not always have the same keys, so d['foo'] or
    d['bar'] may be undefined. I can obviously do something like

    if not 'foo' in d:
    d['foo']="NULL"
    if not 'bar' in d:
    d['bar']="NULL"
    print(f"{id} {d['foo']} {d['bar']}")

    Is there any more compact way of achieving the same thing?

    Cheers,

    Loris

    --
    This signature is currently under constuction.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From duncan smith@21:1/5 to duncan smith on Fri Nov 24 16:54:53 2023
    On 24/11/2023 16:35, duncan smith wrote:
    On 24/11/2023 14:31, Loris Bennett wrote:
    Hi,

    I want to print some records from a database table where one of the
    fields contains a JSON string which is read into a dict.  I am doing
    something like

       print(f"{id} {d['foo']} {d['bar']}")

    However, the dict does not always have the same keys, so d['foo'] or
    d['bar'] may be undefined.  I can obviously do something like

       if not 'foo' in d:
         d['foo']="NULL"
       if not 'bar' in d:
         d['bar']="NULL"
       print(f"{id} {d['foo']} {d['bar']}")

    Is there any more compact way of achieving the same thing?

    Cheers,

    Loris


    Yes. e.g.

    d.get('foo', "NULL")

    Duncan

    Or make d a defaultdict.

    from collections import defaultdict

    dic = defaultdict(lambda:'NULL')
    dic['foo'] = 'astring'
    dic['foo']
    'astring'
    dic['bar']
    'NULL'

    Duncan

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From duncan smith@21:1/5 to Loris Bennett on Fri Nov 24 16:35:14 2023
    On 24/11/2023 14:31, Loris Bennett wrote:
    Hi,

    I want to print some records from a database table where one of the
    fields contains a JSON string which is read into a dict. I am doing something like

    print(f"{id} {d['foo']} {d['bar']}")

    However, the dict does not always have the same keys, so d['foo'] or
    d['bar'] may be undefined. I can obviously do something like

    if not 'foo' in d:
    d['foo']="NULL"
    if not 'bar' in d:
    d['bar']="NULL"
    print(f"{id} {d['foo']} {d['bar']}")

    Is there any more compact way of achieving the same thing?

    Cheers,

    Loris


    Yes. e.g.

    d.get('foo', "NULL")

    Duncan

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From DL Neil@21:1/5 to Loris Bennett via Python-list on Sun Nov 26 08:43:48 2023
    On 11/25/2023 3:31 AM, Loris Bennett via Python-list wrote:
    Hi,

    I want to print some records from a database table where one of the
    fields contains a JSON string which is read into a dict. I am doing something like

    print(f"{id} {d['foo']} {d['bar']}")

    However, the dict does not always have the same keys, so d['foo'] or
    d['bar'] may be undefined. I can obviously do something like

    if not 'foo' in d:
    d['foo']="NULL"
    if not 'bar' in d:
    d['bar']="NULL"
    print(f"{id} {d['foo']} {d['bar']}")

    Is there any more compact way of achieving the same thing?


    What does "the dict does not always have the same keys" mean?

    a) there are two (or...) keys, but some records don't include both;

    b) there may be keys other than 'foo' and 'bar' which not-known in-advance;

    c) something else.


    As mentioned, dict.get() solves one of these.

    Otherwise, there are dict methods which collect/reveal all the keys, all
    the values, or both - dict.keys(), .values(), .items(), resp.

    --
    Regards =dn

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Loris Bennett@21:1/5 to DL Neil on Tue Nov 28 15:06:11 2023
    DL Neil <[email protected]> writes:

    On 11/25/2023 3:31 AM, Loris Bennett via Python-list wrote:
    Hi,
    I want to print some records from a database table where one of the
    fields contains a JSON string which is read into a dict. I am doing
    something like
    print(f"{id} {d['foo']} {d['bar']}")
    However, the dict does not always have the same keys, so d['foo'] or
    d['bar'] may be undefined. I can obviously do something like
    if not 'foo' in d:
    d['foo']="NULL"
    if not 'bar' in d:
    d['bar']="NULL"
    print(f"{id} {d['foo']} {d['bar']}")
    Is there any more compact way of achieving the same thing?


    What does "the dict does not always have the same keys" mean?

    a) there are two (or...) keys, but some records don't include both;

    b) there may be keys other than 'foo' and 'bar' which not-known in-advance;

    c) something else.

    Sorry for being unclear. There is either 'foo' or 'bar' or both, plus
    some other keys which are always present.

    As mentioned, dict.get() solves one of these.

    Otherwise, there are dict methods which collect/reveal all the keys,
    all the values, or both - dict.keys(), .values(), .items(), resp.

    That is a also a good point. I had forgotten about dict.keys() and dict.values(), and hadn't been aware of dict.items().

    Cheers,

    Loris

    --
    This signature is currently under constuction.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Loris Bennett@21:1/5 to duncan smith on Tue Nov 28 14:58:26 2023
    duncan smith <[email protected]d> writes:

    On 24/11/2023 16:35, duncan smith wrote:
    On 24/11/2023 14:31, Loris Bennett wrote:
    Hi,

    I want to print some records from a database table where one of the
    fields contains a JSON string which is read into a dict.  I am doing
    something like

       print(f"{id} {d['foo']} {d['bar']}")

    However, the dict does not always have the same keys, so d['foo'] or
    d['bar'] may be undefined.  I can obviously do something like

       if not 'foo' in d:
         d['foo']="NULL"
       if not 'bar' in d:
         d['bar']="NULL"
       print(f"{id} {d['foo']} {d['bar']}")

    Is there any more compact way of achieving the same thing?

    Cheers,

    Loris

    Yes. e.g.
    d.get('foo', "NULL")
    Duncan

    Or make d a defaultdict.

    from collections import defaultdict

    dic = defaultdict(lambda:'NULL')
    dic['foo'] = 'astring'
    dic['foo']
    'astring'
    dic['bar']
    'NULL'

    Duncan


    I have gone with the 'd.get' solution, as I am just need to print the
    dict to the terminal. The dict is actually from a list of dicts which
    is generated by querying a database, so I don't think the defaultdict
    approach would be so appropriate, but it's good to know about it.

    Thanks,

    Loris
    --
    This signature is currently under constuction.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)