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
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
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?
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.
On 24/11/2023 16:35, duncan smith wrote:
On 24/11/2023 14:31, Loris Bennett wrote:
Hi,Yes. e.g.
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
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
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 715 |
| Nodes: | 16 (2 / 14) |
| Uptime: | 40:07:00 |
| Calls: | 12,109 |
| Files: | 15,006 |
| Messages: | 6,518,395 |