2011-11-14 66 views
0

我有一個我正在使用的文件。我想創建一個讀取文件並將內容放入字典的函數。然後該字典需要通過主函數傳遞。這是主程序。它不能改變。我所做的一切都必須與主程序一起工作。創建打開文件並創建字典的功能

def main(): 
    sunspot_dict = {} 
    file_str = raw_input("Open what data file: ") 
    keep_going = True 
    while keep_going: 
     try: 
      init_dictionary(file_str, sunspot_dict) 
     except IOError: 
      print "Data file error, try again" 
      file_str = raw_input("Open what data file: ")  
      continue 
     print "Jan, 1900-1905:", avg_sunspot(sunspot_dict, (1900,1905),(1,1)) 
     print "Jan-June, 2000-2011:", avg_sunspot(sunspot_dict, (2000,2011), (1,6)) 
     print "All years, Jan:", avg_sunspot(sunspot_dict, month_tuple=(1,1)) 
     print "All months, 1900-1905:", avg_sunspot(sunspot_dict, year_tuple=(1900,1905)) 
     try: 
      print "Bad Year Key example:", avg_sunspot(sunspot_dict, (100,1000), (1,1)) 
     except KeyError: 
      print "Bad Key raised" 
     try: 
      print "Bad Month Index example:", avg_sunspot(sunspot_dict, (2000,2011), (1,100)) 
     except IndexError: 
      print "Bad Range raised" 
     keep_going = False 
    print "Main function finished normally." 

    print sunspot_dict 

這是我到目前爲止有:

def init_dictionary(file_str, sunspot_dict): 
    line = open(file_str, "rU") 
    sunspot_dict = {} 
    for i in line: 
     sunspot_dict[i] 
print sunspot_dict 
+0

@KarlKnechtel我不知道如何繼續。也許我正在理解錯誤的問題。以下是作業內容: init_dictionary(file_string,sunspot_dict)。該函數將參數作爲要打開的文件的名稱和一個空字典。它沒有返回值,但是填充字典的值。關鍵應該是一年;該數據應該是該年的月份的太陽黑子數據列表。 難道是說使用__init__函數在類下執行此操作嗎?或者我應該創建一個名爲init_dictionary(file_string,sunspot_dict)的普通函數? – SimplyZ

回答

1

根據您的第二條評論和您的預覽代碼,您處於正確的軌道上。 Python中的字典通過引用傳遞,因此您應該只能填寫提供給init_dictionary的字典,並且值將可在main()中訪問。在你的情況下,你正在init_dictionary中創建一個新的字典sunspot_dict = {}行。您不想這樣做,因爲您要使用的字典已在main()中創建。

在這一點上,我們需要知道您正在閱讀的文件的格式。基本上你需要打開文件,然後逐行讀取它,同時將這些行解析爲鍵/值對並填入sunspot_dict

+0

這只是一個文本文件。 MONTHLY MEAN SUNSPOT NUMBERS =========================================== ========================= 年份一月二月三月四月五月六月七月八月九月十月十一月十二月 ---------- -------------------------------------------------- -------- 1749 58.0 62.6 70.0 55.7 85.0 83.5 94.8 66.3 75.9 75.5 158.6 85.2 我已經把年份變成了鍵和數字後面的值。現在我需要除去數字以外的所有東西。任何想法,以最好的方式來做到這一點? – SimplyZ

+0

從那裏我需要創建元組。多年來一次,幾個月又一次。這是爲了用戶輸入一定的時間並獲得平均值。 – SimplyZ

+0

你寫了'avg_sunspot'函數嗎?看起來它需要你的字典,然後是兩個元組:一個說明了年的平均範圍,另一個說明了幾個月的範圍。如果您在編寫該功能時遇到困難,則應該提出一個新問題。 – aganders3

1

鑑於你給你的答覆雷蒙德,你不能修改init_dictionary()方法,它要求你通過它的限制一個字典,然後它將填充,我建議你讓你自己的類是dict的一個子類。

class MySunspotDict(dict): 
    def __init__(self, file_str): 
     init_dictionary(file_str, self) 

    # ... define your other methods here 

sunspot_dict = MySunspotDict(file_str) 

sunspot_dict = MySunspotDict(file_str)行雲,你現在有init_dictionary(file_str, sunspot_dict),所以現有的file_str傳遞給MySunspotDict構造(__init__()方法),然後將它傳遞給init_dictionary()

init_dictionary()調用與您已有的調用相同,只是它將新對象傳遞給填充字典的函數,而不是空的dict。由於你的新類是從dict派生的,即它是一個子類,所以它就像普通字典一樣工作。所以你所擁有的基本上是一個dict,你可以附加你自己的方法。 (您也可以通過定義具有特殊名稱的方法來重寫某些標準字典行爲,但在這種情況下您不需要這樣做。)

請注意,在您的方法中,self將是字典 - 您不需要在__init__()中創建字典並將其存儲爲類的屬性(您將在很多Python代碼中看到此模式)。該字典在__init__()運行時已經存在。

+0

由於主要功能是詢問用戶文件的名稱,我如何將它與我正在創建的類連接起來?在你的例子中,主函數會超出MySunspotDict類嗎?你能向我解釋下面的代碼部分嗎? init_dictionary(file_str,self) – SimplyZ

+0

增加了一些額外的說明。 – kindall

+0

「你的sunspot_dict = MySunspotDict(file_str)行是你現在擁有init_dictionary(file_str,sunspot_dict)的地方」 你想說在while循環下改變主函數嗎? while keep_going: 嘗試: init_dictionary(file_str,sunspot_dict) 除IOError: 我無法更改主函數中的任何內容。我寫的代碼必須與主函數一起工作。我需要讀取用戶從主函數輸入的文件並將數據放入字典中。 – SimplyZ