本文介绍了创建一个打开文件并创建字典的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我正在使用的文件。我想创建一个读取文件并将内容放入字典的功能。那么该字典需要通过主函数传递。这是主程序。它不能改变。我所做的一切都必须与主程序一起工作。

  def main():
sunspot_dict = {}
file_str = raw_input(打开什么数据文件)
keep_going = True
while keep_going:
try:
init_dictionary(file_str,sunspot_dict)
除了IOError:
打印数据文件错误,再试一次
file_str = raw_input(打开什么数据文件:)
continue
printJan,1900-1905:,avg_sunspot太阳黑子,(1900,1905),(1,1))
print2000年1月至6月,,avg_sunspot(sunspot_dict,(2000,2011),(1,6))
printAll years,Jan:,avg_sunspot(sunspot_dict,month_tuple =(1,1))
print所有月份,1900-1905:,avg_sunspot(sunspot_dict,year_tuple =(1900,1905))
try:
printBad Year Key example:,avg_sunspot(sunspot_dict,(100,1000),(1,1))
除了KeyError:
printBad Key rais ed
try:
printBad Month Index example:,avg_sunspot(sunspot_dict,(2000,2011),(1,100))
除了IndexError:
printBad Range提高
keep_going = False
print主要功能正常完成。

打印sunspot_dict

这是我到目前为止:


  def init_dictionary(file_str,sunspot_dict):
line = open(file_str,rU)
sunspot_dict = { $ $ $ $ $ $ $ $ $ $ $ $ $
$ / $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ = $ = = = = = = = = = = = = = = = = = = = = = h2_lin>解决方案

根据您的第二条评论和预览代码,您处于正确的轨道。 Python中的字典通过引用传递,所以您应该可以填写提供给 init_dictionary 的字典,并且值可以在 main() 。在您的情况下,您将在 init_dictionary 中使用行 sunspot_dict = {} 创建一个新字典。您不想这样做,因为您要使用的字典已经在 main()中创建。



在这一点上,我们需要知道你正在阅读的文件的格式。基本上你需要打开这个文件,然后可以一行一行地读取它们,然后将这些行解析成键/值对并填写 sunspot_dict


I have a file that I'm working with. I want to create a function that reads the files and place the contents into a dictionary. Then that dictionary needs to be passed through the main function. Here is the main program. It cannot be changed. Everything I do must work with the main program.

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

Here is what I have so far:

def init_dictionary(file_str, sunspot_dict):
    line = open(file_str, "rU")
    sunspot_dict = {}
    for i in line:
        sunspot_dict[i]
print sunspot_dict
解决方案

Based on your second comment and your preview code, you are on the right track. Dictionaries in Python are passed by reference, so you should just be able to fill the dictionary provided to init_dictionary and the values will be accessible in main(). In your case you are creating a new dictionary in init_dictionary with the line sunspot_dict = {}. You don't want to do this, because the dictionary you want to use was already created in main().

At this point we would need to know the format of the file you are reading. Basically you need to open the file, then probably read it line by line while parsing the lines into key/value pairs and filling sunspot_dict.

这篇关于创建一个打开文件并创建字典的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 05:32