Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.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_Python 2.7 - Fatal编程技术网

Python 如何从字典中获取特定值?

Python 如何从字典中获取特定值?,python,python-2.7,Python,Python 2.7,我有一个函数,它接收两个文件.txt,一个带有任务,另一个带有transaltor。 向翻译人员返回已分配任务的列表 无论如何,这不是重点,我试图检查translators字典上的给定值是否等于tasks文件中的另一个元素(列表),这是函数btw: def scheduleTasks(translators, tasks, date, hour, periodAfter): """Assigns translation tasks to translators. Requir

我有一个函数,它接收两个文件.txt,一个带有任务,另一个带有transaltor。 向翻译人员返回已分配任务的列表

无论如何,这不是重点,我试图检查translators字典上的给定值是否等于tasks文件中的另一个元素(列表),这是函数btw:

def scheduleTasks(translators, tasks, date, hour, periodAfter):

    """Assigns translation tasks to translators.

    Requires:
    translators is a dict with a structure as in the output of
    readingFromFiles.readTranslatorsFile concerning the update time; 
    tasks is a list with the structure as in the output of 
    readingFromFiles.readTasksFile concerning the period of
    periodAfter minutes immediately after the update time of translators;
    date is string in format DD:MM:YYYY with the update time;
    hour is string in format HH:MN: with the update hour;
    periodAfter is a int with the number of minutes of a period 
    of time elapsed.
    Ensures:
    a list of translation tasks assigned according to the conditions
    indicated in the general specification (omitted here for 
    the sake of readability).
    """
我的问题是如何从字典中获取值,我知道d.values(),但这会返回所有值,如(这是一个键的值,举个例子):

,但我只想要(葡萄牙语;法语)一点,如果你想跑像

for translator in translators.values():
      print translator[0]
它返回“[”,我不确定,但我认为这是由于linux写文件的方式,这是我用来读取文件的函数:

def readTranslatorsFile(file_name):
    """Reads a file with a list of translators into a collection.

    Requires:
    file_name is str with the name of a .txt file containing
    a list of translators organized as in the examples provided in
    the general specification (omitted here for the sake of readability).
    Ensures:
    dict where each item corresponds to a translator listed in
    file with name file_name, a key is the string with the name of a translator,
    and a value is the list with the other elements belonging to that
    translator, in the order provided in the lines of the file.
    """
    inFile = removeHeader(file_name)       

    translatorDict = {}
    for line in inFile:

        key = line.split(",")[INDEXTranslatorName]
        value = line.split(",")[1::]
        translatorDict[key] = str(value)
    return translatorDict
这是输入文件:

Company:
ReBaBel
Day:
07:11:2016
Time:
23:55
Translators:
Ana Tavares, (english), (portuguese), 1*, 0.501, 2000, 20000, 2304, 08:11:2016
Mikolás Janota, (czech), (english; portuguese), 3*, 1.780, 2000, 200000, 4235, 08:11:2016
Paula Guerreiro, (french), (portuguese), 2*, 0.900, 3500, 45000, 21689, 11:11:2016
Peter Wittenburg, (dutch; english), (dutch; english), 2*, 1.023, 2500, 20000, 7544, 08:11:2016
Rita Carvalho, (english), (portuguese), 1*, 0.633, 5000, 400000, 18023, 09:11:2016
Steven Neale, (portuguese; french), (english), 3*, 0.803, 3000, 25000, 3084, 08:11:2016

问题是,在解析文件时,您使用
str.split()
创建了一个列表,这很好,但随后您将此列表转换回字符串,而不是将
list
保留为您的值。这很糟糕

translatorDict[key] = str(value)
我会这样做:

  • 分道扬镳
  • 放弃标题(字段不足:列表索引超出范围)
  • 将字典值存储为令牌列表,更加灵活
代码:

(或使用
csv
模块更正确地处理逗号分隔的文件(引用等))

请注意,如果
INDEXTranslatorName
不为零,则您的方法将失败,然后您可以编写
value=tokens[:INDEXTranslatorName]+tokens[INDEXTranslatorName+1://code>或
tokens.pop(INDEXTranslatorName)

然后:


高效地打印您的语言元组。

@Jean-Françoisfar我知道它仍然会给我输出,只需要部分值。请尝试
列表(translators.values())[0]
下一步(translators.values()。uu iter_uuu())
@Jean-Françoisfar第一个返回[一行和]在下一个;第二个,它只是一个空白行,正如我所想的,因为输出是一个包含这些值的字符串的列表,所以当我试图从那里打印一些东西时,它会给我类似“或[当我尝试for循环时,它会给我超出范围的索引o.ob,但如果我只是打印translator,它会给我包含值的列表,所以我不知道为什么[0]给我超出范围的索引必须是因为标题。编辑代码以过滤标题。是的,就是这样,如果我想比较这些语言和任务文件中的语言,我如何删除这些语言上的(),或者过滤它们,因为我从tasks.txt获得的语言没有它们
translatorDict[key] = str(value)
def readTranslatorsFile(file_name):

    inFile = removeHeader(file_name)       

    translatorDict = {}
    for line in inFile:
        tokens = line.split(",")  # split once and for good
        if len(tokens)>1:
          key = tokens[INDEXTranslatorName]
          value = tokens[1:]  # all items but first
          translatorDict[key] = value
    return translatorDict
for translator in translators.values():
      print(translator[0].strip().replace(")","").replace("(",""))