可能重复:
Create a function that opens a file and creates a dictionary
我们有一个主要功能用于:
编写以下两个函数:
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年的数字填充数字
答案 0 :(得分:0)
在Python中阅读有关IO的更多信息....
您需要打开文件,提取其内容,然后填充您的字典......
提示是看这个例子,他们试图从文件中创建一个字典.... reading file to dictionary