我有一个接收两个文件.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写入文件的方式,这是Im用来读取文件的函数:
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发布于 2016-12-19 21:20:08
问题是,当您解析文件时,您使用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("(",""))高效地打印您的语言元组。
https://stackoverflow.com/questions/41223342
复制相似问题