如何根据特定参数将多个 key:value 字典分配到已建立的字典中
How to assign multiple key:value dicts into an already established dictionary, according to specific parameter
注意:这使用了一个名为 PRAW 的单独库,这对于理解问题并不重要,ambiguous/related 代码在我下面的示例中已用 # !!!
注释以表示只有在您需要理解代码 生成字典列表 时才需要该代码。
*问题有几层深——我会尽力解释:
我在 data.json
中有 JSON 数据,看起来像这样:
{
"USA":[
{"shortlink":"https://short/74h13v"},
{"responses":[]}
],
"Vietnam":[
{"shortlink":"https://short/74gyn4"},
{"responses":[]}
],
"Italy":[
{"shortlink":"https://short/74h3i9"},
{"responses":[]}
]
}
在模块 (scraper.py
) 中,我有额外的数据,这些数据将以 comment.id="39dn28"
、comment.body="this is a comment"
的形式出现
我正在尝试将多个 comment.id
和 comment.body
实例插入到附加到 responses
的 []
中,因此它看起来像这样:
{"responses": [
{"39dn28": "this is my response"},
{"39k229": "I'm another response"},
{"35sn64": "another comment"}
]}
对我来说特别棘手的是,当我必须考虑每组评论与单个国家/地区的 ID(或 'shortlink')相匹配时。我用 shortlinks = [data[link][0]["shortlink"][-6:] for link in data]
提取了短链接 ID,结果是 ['74h3i9', '74gyn4', '74h13v']
.
现在我需要将每组评论与其对应的短链接相匹配,并将这些评论输入到它们正确所属的位置。
以下是我到目前为止所做的尝试,以深入了解我所拥有的和我正在努力完成的事情:
with open("data.json", "r") as f:
data = json.load(f)
shortlinks = [data[link][0]["shortlink"][-6:] for link in data]
for sl_id in shortlinks:
# !!! (the following code produces a list of comment dicts.)
submission = reddit.submission(id=sl_id)
submission.comments.replace_more(limit=0)
cmt_data = [{comment.id: comment.body} for comment in submission.comments.list()]
for i in data:
if sl_id in data[i][0]["shortlink"]:
data[i][0]["responses"] = cmt_data
print(data)
这几乎 有效。出于某种原因,我还返回了额外的空白 'responses': []
和额外的短链接。
似乎无法弄清楚。帮助很大,非常感谢。我愿意接受其他方法来完成它,以及存储数据的其他方法(可能不是字典列表等)。
for key, value in data.items():
for x in value:
if sl_id in x['shortlink']:
if not 'responses' in x:
x['responses'] = []
x['responses'] += cmt_data
如果你想得到这样的东西:
{
"USA":[
{"shortlink":"https://short/74h13v"},
{"responses":[{},{},{}]}],...]
}
我想,应该是这样的:
for sl_id in shortlinks:
# !!! (the following code produces a list of comment dicts.)
submission = reddit.submission(id=sl_id)
submission.comments.replace_more(limit=0)
cmt_data = [{comment.id: comment.body} for comment in submission.comments.list()]
for i in data:
if sl_id in data[i][0]["shortlink"]:
data[i][1]["responses"] = cmt_data
注意:这使用了一个名为 PRAW 的单独库,这对于理解问题并不重要,ambiguous/related 代码在我下面的示例中已用 # !!!
注释以表示只有在您需要理解代码 生成字典列表 时才需要该代码。
*问题有几层深——我会尽力解释:
我在 data.json
中有 JSON 数据,看起来像这样:
{
"USA":[
{"shortlink":"https://short/74h13v"},
{"responses":[]}
],
"Vietnam":[
{"shortlink":"https://short/74gyn4"},
{"responses":[]}
],
"Italy":[
{"shortlink":"https://short/74h3i9"},
{"responses":[]}
]
}
在模块 (scraper.py
) 中,我有额外的数据,这些数据将以 comment.id="39dn28"
、comment.body="this is a comment"
我正在尝试将多个 comment.id
和 comment.body
实例插入到附加到 responses
的 []
中,因此它看起来像这样:
{"responses": [
{"39dn28": "this is my response"},
{"39k229": "I'm another response"},
{"35sn64": "another comment"}
]}
对我来说特别棘手的是,当我必须考虑每组评论与单个国家/地区的 ID(或 'shortlink')相匹配时。我用 shortlinks = [data[link][0]["shortlink"][-6:] for link in data]
提取了短链接 ID,结果是 ['74h3i9', '74gyn4', '74h13v']
.
现在我需要将每组评论与其对应的短链接相匹配,并将这些评论输入到它们正确所属的位置。
以下是我到目前为止所做的尝试,以深入了解我所拥有的和我正在努力完成的事情:
with open("data.json", "r") as f:
data = json.load(f)
shortlinks = [data[link][0]["shortlink"][-6:] for link in data]
for sl_id in shortlinks:
# !!! (the following code produces a list of comment dicts.)
submission = reddit.submission(id=sl_id)
submission.comments.replace_more(limit=0)
cmt_data = [{comment.id: comment.body} for comment in submission.comments.list()]
for i in data:
if sl_id in data[i][0]["shortlink"]:
data[i][0]["responses"] = cmt_data
print(data)
这几乎 有效。出于某种原因,我还返回了额外的空白 'responses': []
和额外的短链接。
似乎无法弄清楚。帮助很大,非常感谢。我愿意接受其他方法来完成它,以及存储数据的其他方法(可能不是字典列表等)。
for key, value in data.items():
for x in value:
if sl_id in x['shortlink']:
if not 'responses' in x:
x['responses'] = []
x['responses'] += cmt_data
如果你想得到这样的东西:
{
"USA":[
{"shortlink":"https://short/74h13v"},
{"responses":[{},{},{}]}],...]
}
我想,应该是这样的:
for sl_id in shortlinks:
# !!! (the following code produces a list of comment dicts.)
submission = reddit.submission(id=sl_id)
submission.comments.replace_more(limit=0)
cmt_data = [{comment.id: comment.body} for comment in submission.comments.list()]
for i in data:
if sl_id in data[i][0]["shortlink"]:
data[i][1]["responses"] = cmt_data