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

时间:2016-12-19 13:10:21

标签: python python-2.7

我有一个函数可以收到两个文件.txt,一个有任务,另一个有transaltors。 返回已分配给翻译者的任务列表。

无论如何,这不是重点,我试图检查翻译词典上的给定值是否等于任务文件中列出的另一个元素,这是函数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()但是这会返回所有值(例如,这是一个键中的值,仅作为示例):

[' (portuguese; french)', ' (english)', ' 3*', ' 0.803', ' 3000', ' 25000', ' 3084', ' 08:11:2016\\n']

,但我只想要(葡萄牙语;法语)位,如果尝试运行类似

的东西
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

1 个答案:

答案 0 :(得分:1)

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

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

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

请注意,如果INDEXTranslatorName不为零,您的方法会失败,那么您可以写value = tokens[:INDEXTranslatorName]+tokens[INDEXTranslatorName+1:]tokens.pop(INDEXTranslatorName)

然后:

for translator in translators.values():
      print(translator[0].strip().replace(")","").replace("(",""))

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

相关问题