【发布时间】:2011-11-14 20:17:40
【问题描述】:
可能重复:
Create a function that opens a file and creates a dictionary
我们被赋予了一个 main 函数用于:
编写以下两个函数:
-
init_dictionary(file_string, sunspot_dict)
此函数将要打开的文件名和一个空字典作为参数。它没有返回值,但确实用值填充字典。关键应该是年份;数据应该是当年月份的太阳黑子数据列表。
主要功能是:
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."
我不知道从哪里开始!请帮忙!
给出的文件是 x 轴上的月份和 1749 年至 2011 年的年份,其中填有数字
【问题讨论】:
-
“我不知道从哪里开始!”您需要从某个地方开始,所以从打开和读取文件开始(如果您不知道如何,请搜索如何在 Python 中进行文件 I/O)。一旦遇到具体问题,提出问题,如果运气好的话,也许有人可以提供帮助。
标签: python