2016-12-19 34 views
1

我有一個函數可以接收兩個文件.txt,一個是任務,另一個是transaltors。 將已分配任務的列表返回給譯員。如何從字典中獲得特定值?

反正那不是重點,我試着去檢查,如果在翻譯字典中的給定值等於出現在任務文件,該文件是一個列表中的其他元素,這是者均基於功能:

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 
+1

@ Jean-FrançoisFabre我知道它仍然給我那個輸出,只是想要一部分的價值。 – Exzlanttt

+0

嘗試'list(translators.values())[0]'或'next(translators.values().__ iter __())' –

+0

@ Jean-FrançoisFabre第一個返回[in one line and] on next;第二個它只是空行 – Exzlanttt

回答

1

的問題是,當您分析您的文件創建使用str.split()這是很好的一個列表,但你轉換回這個列表作爲字符串,而不是保留list爲您值。那很不好。

translatorDict[key] = str(value) 

我會做到這一點:

  • 分割線
  • 丟棄頭(沒有足夠的字段:列表索引超出範圍)
  • 存儲字典值作爲憑證清單,更靈活

code:

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("(","")) 

高效打印語言的元組。

+0

這就是我雖然,因爲輸出是一個列表與這些值的字符串,爲什麼當我試圖從那裏打印的東西它給我的東西像'或只是[ – Exzlanttt

+0

當我嘗試for循環它給了我索引超出範圍o。 – Exzlanttt

+0

但是,如果我只是打印翻譯它給我列表裏面的值,所以我不知道爲什麼[0]給我索引超出範圍 – Exzlanttt