我正在製作一個代碼,它從JSON文件中隨機選取信息並將其放入applescript顯示通知中。並且可以通過終端運行JSON文件的問題
我想在我的JSON文件中創建三個不同的列表,它們都鏈接到那裏一件事:random_name,random_sentence,random_sub而不是隻有一個列表,並且只從該列表中選取所有單詞。
我該怎麼做?我應該用字典來做這件事嗎?變量?製作其他JSON文件?
的Python文件:
#!/usr/bin/python
import json
import random
import subprocess
def randomLine():
jsonfile = "sentences.json"
with open(jsonfile) as data_file:
data = json.load(data_file)
# print len(data)
return random.choice(data)
def executeShell(notif_string, notif_title, notif_subtitle):
applescript = 'display notification "%s" with title "%s" subtitle "%s"' % (notif_string, notif_title, notif_subtitle)
subprocess.call(["osascript", "-e", applescript])
def main():
random_name = randomLine()
random_zin = randomLine()
random_sub = randomLine()
executeShell(random_name, random_zin, random_sub)
if __name__ == '__main__':
main()
JSON文件:
[
"one",
"two",
"three",
"four",
"five",
"six"
]
嗯,我不明白。您想做什麼?期望的輸出是什麼?你能給我們一個[mcve]嗎? –
使用此代碼,所有字符串都會從JSON文件中的一個列表中選擇隨機單詞,例如「one」,「two」,「three」。我很想在我的JSON文件中有與我的python文件中的字符串鏈接的單獨列表。例如,第一個字符串從列表中隨機選擇單詞「one」,「two」,「three」,第二個字符串從另一個列表中隨機選擇,例如:「aa」,「bb」,「cc」。 – Danisk