Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python JSON文件的问题_Python_Json_String_List_Variables - Fatal编程技术网

Python JSON文件的问题

Python JSON文件的问题,python,json,string,list,variables,Python,Json,String,List,Variables,我正在编写一个代码,从JSON文件中随机选取信息,并将其放入applescript显示通知中。并且可以通过终端运行 我想在我的JSON文件中列出三个不同的列表,所有这些列表都链接到一个东西:random_name、random_句子、random_sub,而不是只有一个列表并从其中挑选所有单词 我该怎么做?我应该做一本字典吗?变量?制作其他JSON文件 Python文件: #!/usr/bin/python import json import random import subprocess

我正在编写一个代码,从JSON文件中随机选取信息,并将其放入applescript显示通知中。并且可以通过终端运行

我想在我的JSON文件中列出三个不同的列表,所有这些列表都链接到一个东西:random_name、random_句子、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"
]

这应该可以完成任务
json.load()
返回一个字典,因此您可以在字典上运行
random.choice()

import json
import random
import subprocess

jsonfile = "sentences.json"

def main():
    with open(jsonfile, "r") as file:
        # You might want to add a sanity check here, file might be malformed
        data = json.load(file)

    random_name = random.choice(data["name"])
    random_zin = random.choice(data["zin"])
    random_sub = random.choice(data["sub"])
    subprocess.call(["osascript", "-e", "display notification \
        '{0}' with title '{1}' \
        subtitle '{2}'".format(random_name, random_zin, random_sub)])

if __name__ == '__main__':
    main()
原始JSON文件只包含一个字符串列表。这是一个有3个不同列表的列表,分别名为“name”、“zin”和“sub”:


我不明白。你想做什么?预期输出是什么?你能给我们一个简单的例子吗?用这个代码,所有的字符串从JSON文件中的一个列表中随机抽取单词,比如“一”、“二”、“三”。我希望在JSON文件中有单独的列表,这些列表与python文件中的字符串链接。例如,第一个字符串从带有“一”、“二”、“三”的列表中随机选择单词,第二个字符串从另一个列表中随机选择单词,例如:“aa”、“bb”、“cc”。也许你应该注意原始JSON文件的问题,我是说,为了让你的答案有用,你应该在回答中注意JSON文件的问题谢谢你的回答!也许我做错了什么,但它说:执行错误:变量2没有定义。你知道我能做些什么来修复这个问题吗?@s0r00tI不在OS X上,但是您可能希望在
subprocess.call()
中的格式化字符串({0}…)上添加引号。编辑我的答案后,代码应该可以工作。
{
    "name":[
        "one",
        "two",
        "three"
     ],
     "zin":[
        "four",
        "five",
        "six"
     ],
     "sub":[
        "seven",
        "eight",
        "nine"
    ]
}