如何从 OrderedDict 中提取特定值?
How do I extract specific values from an OrderedDict?
我喜欢提取特定的键并将它们存储到列表中。到目前为止,我能够从 MariaDB 读取并将行存储为字典(我更喜欢将输出作为 JSON):
import pymysql
import simplejson as json
import collections
import credentials_global
conn = pymysql.connect(
host=credentials_global.mariadb_dev_ip_address,
user=credentials_global.mariadb_dev_username,
password=credentials_global.mariadb_dev_password,
port=credentials_global.mariadb_dev_port,
database=credentials_global.mariadb_dev_db_ticketing,
)
cursor = conn.cursor()
cursor.execute("select a, b, c, d, e, f from master.orders where c = 215")
rows = cursor.fetchall()
objects_list = []
for row in rows:
d = collections.OrderedDict()
d["a"] = row[0]
d["b"] = row[1]
d["c"] = row[2]
d["d"] = row[3]
d["e"] = row[4]
d["f"] = row[5]
objects_list.append(d)
j = json.dumps(objects_list)
print(j)
这会产生输出:
[
{
"a": 4153,
"b": "NO_EFFECT",
"c": "none",
"d": "Medium",
"e": 1,
"f": "No Remarks",
},
{
"a": 4154,
"b": "SIGNIFICANT",
"c": "none",
"d": "Low",
"e": 1,
"f": "Test Message",
},
]
我喜欢提取所有出现的 f
。我试过:
for key, value in d.items():
print(value)
这输出:
4153
NO_EFFECT
none
Medium
1
No Remarks
4154
SIGNIFICANT
none
Low
1
Test Message
我更喜欢只提取 f
以便输出类似于 [No Remarks, Test Message]
(我假设序列保持不变)。有人可以帮助我如何实现或在哪里寻找吗?
谢谢
for obj in objects_list:
print(obj['f'])
OrderedDict 保留键的顺序 (a b c ...)。此输出中的顺序来自 objects_list.
中的顺序
将其添加到输出列表中:
only_f_fields = [ obj['f'] for obj in objects_list ]
我想你要找的答案是
for key, value in d.items():
print(value[0]['f'])
列表中的第一个元素 0,以及该列表中字典中的元素 f。
假设接收到的输出是一个列表:
ex = [{"a": 4153, "b": "NO_EFFECT", "c": "none", "d": "Medium", "e": 1, "f": "No Remarks"},
{"a": 4154, "b": "SIGNIFICANT", "c": "none", "d": "Low", "e": 1, "f": "Test Message"}]
我们可以执行两种方式,一种迭代通常不假设键顺序,另一种迭代顺序。
正常迭代检查(仅作说明)
f_list = list()
for val in ex:
for k, v in val.items():
if k == "f":
f_list.append(v)
print(f_list)
输出:
['No Remarks', 'Test Message']
有序字典保持键的顺序:
f_list = list()
for val in ex:
f_list.append(val["f"])
print(f_list)
输出:
['No Remarks', 'Test Message']
我喜欢提取特定的键并将它们存储到列表中。到目前为止,我能够从 MariaDB 读取并将行存储为字典(我更喜欢将输出作为 JSON):
import pymysql
import simplejson as json
import collections
import credentials_global
conn = pymysql.connect(
host=credentials_global.mariadb_dev_ip_address,
user=credentials_global.mariadb_dev_username,
password=credentials_global.mariadb_dev_password,
port=credentials_global.mariadb_dev_port,
database=credentials_global.mariadb_dev_db_ticketing,
)
cursor = conn.cursor()
cursor.execute("select a, b, c, d, e, f from master.orders where c = 215")
rows = cursor.fetchall()
objects_list = []
for row in rows:
d = collections.OrderedDict()
d["a"] = row[0]
d["b"] = row[1]
d["c"] = row[2]
d["d"] = row[3]
d["e"] = row[4]
d["f"] = row[5]
objects_list.append(d)
j = json.dumps(objects_list)
print(j)
这会产生输出:
[
{
"a": 4153,
"b": "NO_EFFECT",
"c": "none",
"d": "Medium",
"e": 1,
"f": "No Remarks",
},
{
"a": 4154,
"b": "SIGNIFICANT",
"c": "none",
"d": "Low",
"e": 1,
"f": "Test Message",
},
]
我喜欢提取所有出现的 f
。我试过:
for key, value in d.items():
print(value)
这输出:
4153
NO_EFFECT
none
Medium
1
No Remarks
4154
SIGNIFICANT
none
Low
1
Test Message
我更喜欢只提取 f
以便输出类似于 [No Remarks, Test Message]
(我假设序列保持不变)。有人可以帮助我如何实现或在哪里寻找吗?
谢谢
for obj in objects_list:
print(obj['f'])
OrderedDict 保留键的顺序 (a b c ...)。此输出中的顺序来自 objects_list.
中的顺序将其添加到输出列表中:
only_f_fields = [ obj['f'] for obj in objects_list ]
我想你要找的答案是
for key, value in d.items():
print(value[0]['f'])
列表中的第一个元素 0,以及该列表中字典中的元素 f。
假设接收到的输出是一个列表:
ex = [{"a": 4153, "b": "NO_EFFECT", "c": "none", "d": "Medium", "e": 1, "f": "No Remarks"},
{"a": 4154, "b": "SIGNIFICANT", "c": "none", "d": "Low", "e": 1, "f": "Test Message"}]
我们可以执行两种方式,一种迭代通常不假设键顺序,另一种迭代顺序。
正常迭代检查(仅作说明)
f_list = list()
for val in ex:
for k, v in val.items():
if k == "f":
f_list.append(v)
print(f_list)
输出:
['No Remarks', 'Test Message']
有序字典保持键的顺序:
f_list = list()
for val in ex:
f_list.append(val["f"])
print(f_list)
输出:
['No Remarks', 'Test Message']