我一直在尝试编写一个程序,该程序将从问题列表中随机选择一个问题,然后显示问题本身。该信息存储在.json
文件中。
这是我当前的代码:
import json
import random
with open("C:\\LearningArabic\\LiblibArriby\\Lessons\\L1_Letters\\L1_Letters.json", "r", encoding = "utf-8-sig") as read_file:
data = json.load(read_file)
t = data["letters"]["part one"]["meem"]["questions"]
print(t) #This prints all of the necessary information. Not actually necessary but it helps me know that this section of code is working
print("")
print(t.get(random.choice(("question"))))
print("")
最终,我会对其进行扩展,但这只是为了使该想法起作用。
这是我的.json
文件:
{"letters":
{"part one": {
"meem": {
"questions": {
"question": "What sound does 'م' make?",
"arabic": "م",
"transliteration": "m",
"english": "m",
"answer": "m",
"wronganswer" : ["a", "b", "kh", "ta", "da", "du",
"ee", "yeh", "he", "ha", "l", "n", "f"]},
"questions": {
"question": "What letters are in the word 'وز'?",
"arabic": "موز",
"transliteration": "mooz",
"english": "banana",
"answer": "م و ز",
"wronganswer": [
"ه و ز",
"ه و ر",
"م ف ز",
"م و ر",
"ه ف ر"]},
"questions": {
"question": "What word do the letters 'ه ر م' make",
"arabic": "ه ر م",
"transliteration": "haram",
"english": "pyramid",
"answer": "هرم",
"wronganswer": [
"مرم",
"هزم",
"موم",
"مرع",
"هبم"]}
}
}}
}
运行代码时,我得到以下信息:
{'question': "What word do the letters 'ه ر م' make", 'arabic': 'ه ر م', 'transliteration': 'haram', 'english': 'pyramid', 'answer': 'هرم', 'wronganswer': ['مرم', 'هزم', 'موم', 'مرع', 'هبم']}
None
主要问题:
当我print(t)
时,我应该可以打印所有问题。相反,我只得到最后一个问题的打印输出。当我尝试打印随机问题时,我得到了“无”的结果,我不理解。我的列表都已填充,为什么python无法找到任何东西?我的主要目标是学习如何从json文件中选择随机值-如果需要更新json文件格式,请告诉我。
答案 0 :(得分:2)
由于您的JSON结构包含相同密钥的多个条目,因此您应该使用自定义JSON解码器,该解码器通过将相同密钥的值转换为列表来解决密钥冲突:
If platform = ‘IOS’
&Variavel = 1
Else if platform = ‘ANDROID’
&VARIAVEL = 2
endif
以使from json import JSONDecoder
def parse_object_pairs(pairs):
d = {}
converted_keys = set()
for key, value in pairs:
if key not in d:
d[key] = value
elif key in converted_keys:
d[key].append(value)
else:
d[key] = [d[key], value]
converted_keys.add(key)
return d
decoder = JSONDecoder(object_pairs_hook=parse_object_pairs)
data = decoder.decode(read_file.read())
中的questions
键成为字典列表:
data
,以便您可以轻松地使用列表上的{'letters': {'part one': {'meem': {'questions': [{'answer': 'm',
'arabic': 'م',
'english': 'm',
'question': 'What sound does '
"'م' make?",
'transliteration': 'm',
'wronganswer': ['a',
'b',
'kh',
'ta',
'da',
'du',
'ee',
'yeh',
'he',
'ha',
'l',
'n',
'f']},
{'answer': 'م و ز',
'arabic': 'موز',
'english': 'banana',
'question': 'What letters '
'are in the word '
"'وز'?",
'transliteration': 'mooz',
'wronganswer': ['ه و ز',
'ه و ر',
'م ف ز',
'م و ر',
'ه ف ر']},
{'answer': 'هرم',
'arabic': 'ه ر م',
'english': 'pyramid',
'question': 'What word do '
"the letters 'ه "
"ر م' make",
'transliteration': 'haram',
'wronganswer': ['مرم',
'هزم',
'موم',
'مرع',
'هبم']}]}}}}
来选择要输出的问题:
random.choice
此输出(随机):
print(random.choice(data['letters']["part one"]["meem"]["questions"]))
答案 1 :(得分:1)
我们不能说JSON无效,但是您应该避免使用类似的key
名称(在您的示例中为questions
)
RFC 7159解释了为什么唯一名称(键)很好:
名称全都是唯一的对象在某种意义上是可以互操作的 所有接收该对象的软件实现都将达成共识 名称-值映射。当对象中的名称不是 唯一的是,接收此类对象的软件的行为是 不可预测的。许多实现报告姓氏/值对 只要。其他实现报告错误或无法解析 对象,某些实现报告所有名称/值对, 包括重复项。
已经发现JSON解析库在是否或 他们不是使对象成员的排序对调用可见 软件。行为不依赖于成员的实现 从某种意义上说,订购将是可互操作的 受这些差异的影响。
您可以尝试将JSON转换为给定格式:
{
"letters": {
"part one": {
"meem": {
"questions": [
{
"question": "What sound does 'م' make?",
"arabic": "م",
"transliteration": "m",
"english": "m",
"answer": "m",
"wronganswer": [
"a",
"b",
"kh",
"ta",
"da",
"du",
"ee",
"yeh",
"he",
"ha",
"l",
"n",
"f"
]
},
{
"question": "What letters are in the word 'وز'?",
"arabic": "موز",
"transliteration": "mooz",
"english": "banana",
"answer": "م و ز",
"wronganswer": [
"ه و ز",
"ه و ر",
"م ف ز",
"م و ر",
"ه ف ر"
]
},
{
"question": "What word do the letters 'ه ر م' make",
"arabic": "ه ر م",
"transliteration": "haram",
"english": "pyramid",
"answer": "هرم",
"wronganswer": [
"مرم",
"هزم",
"موم",
"مرع",
"هبم"
]
}
]
}
}
}
}
然后,您可以执行以下操作从问题列表中提取随机问题:
import json
from random import randint
def random_question():
with open('result.json') as fp:
data = json.load(fp)
questions = data["letters"]["part one"]["meem"]["questions"]
random_index = randint(0, len(questions)-1)
return questions[random_index]['question']
print(random_question())
输出
What sound does 'م' make?
要么
What letters are in the word 'وز'?