Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/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中从多个字典中获取一个键的值?_Python_Loops_Dictionary - Fatal编程技术网

如何在python中从多个字典中获取一个键的值?

如何在python中从多个字典中获取一个键的值?,python,loops,dictionary,Python,Loops,Dictionary,这是我的data.txt文件中的数据 如何才能仅从中的每个设置获取数据 使用循环的词典 谢谢我不确定您首先是如何将词典放入文本文件的,但如果可以删除尾随逗号,即 {"setup": "test", "punchline": "ok", "numOfRatings": 0, "sumOfRatings": 0} {"setup": "test2", "punchline": "ok2", "numOfRatings": 0, "sumOfRatings": 0} 像这样的东西可能适合你: def

这是我的data.txt文件中的数据

如何才能仅从中的每个设置获取数据 使用循环的词典


谢谢

我不确定您首先是如何将词典放入文本文件的,但如果可以删除尾随逗号,即

{"setup": "test", "punchline": "ok", "numOfRatings": 0, "sumOfRatings": 0}
{"setup": "test2", "punchline": "ok2", "numOfRatings": 0, "sumOfRatings": 0}
像这样的东西可能适合你:

def dicts_from_file(file):
    dicts_from_file = []
    with open(file,'r') as inf:
        for line in inf:
            dicts_from_file.append(eval(line))
    return dicts_from_file

def get_setups(dicts):
    setups = []
    for dict in dicts:
        for key in dict:
            if key == "setup":
                setups.append(dict[key])
    return setups

print get_setups(dicts_from_file("data.txt"))

我不确定你首先是如何将字典放入文本文件的,但如果有可能删除尾随的逗号,即

{"setup": "test", "punchline": "ok", "numOfRatings": 0, "sumOfRatings": 0}
{"setup": "test2", "punchline": "ok2", "numOfRatings": 0, "sumOfRatings": 0}
像这样的东西可能适合你:

def dicts_from_file(file):
    dicts_from_file = []
    with open(file,'r') as inf:
        for line in inf:
            dicts_from_file.append(eval(line))
    return dicts_from_file

def get_setups(dicts):
    setups = []
    for dict in dicts:
        for key in dict:
            if key == "setup":
                setups.append(dict[key])
    return setups

print get_setups(dicts_from_file("data.txt"))
对于这段代码,您需要在每行后面加上“,”,因为ast.literal\u evalline将行转换为元组

如果你在每次口述后都没有“,”,那么就用这个

f = open('data')
for line in f:
   d = ast.literal_eval(line)
   print d['setup']
对于这段代码,您需要在每行后面加上“,”,因为ast.literal\u evalline将行转换为元组

如果你在每次口述后都没有“,”,那么就用这个

f = open('data')
for line in f:
   d = ast.literal_eval(line)
   print d['setup']

如果文件中的行是标准dict字符串,则可以尝试此操作

def get_setup_from_file(file_name):
    result = []
    f = open(file_name, "r")
    for line in f.xreadlines():
        # or  line_dict = json.loads(line)
        line_dict = eval(line)  # if line end witch ',', try eval(line[0:-1])
        result.append(line_dict["setup"])
    return result

希望这能对您有所帮助。

如果文件中的行是标准dict字符串,您可以尝试此操作

def get_setup_from_file(file_name):
    result = []
    f = open(file_name, "r")
    for line in f.xreadlines():
        # or  line_dict = json.loads(line)
        line_dict = eval(line)  # if line end witch ',', try eval(line[0:-1])
        result.append(line_dict["setup"])
    return result

希望这能对您有所帮助。

如果是标准dict字符串,请尝试以下操作:

with open(file,'r') as file_input:
    for line in file_input:
        print eval(line).get("setup")

如果是标准dict字符串,请尝试以下操作:

with open(file,'r') as file_input:
    for line in file_input:
        print eval(line).get("setup")

你试过什么了吗?这些数据是JSON格式的吗?看起来是这样。如果是,您是否尝试过使用该模块?如果不起作用,错误消息或问题是什么?展示您迄今为止所做的尝试有助于读者了解您需要什么样的指导。您尝试过什么吗?这些数据是JSON格式的吗?看起来是这样。如果是,您是否尝试过使用该模块?如果不起作用,错误消息或问题是什么?展示你迄今为止所做的尝试有助于读者了解你需要什么样的指导。