Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从文件制作词典_Python - Fatal编程技术网

Python 从文件制作词典

Python 从文件制作词典,python,Python,可能重复: 我们可以使用一个主要函数: 编写以下两个函数: 初始化字典文件字符串,太阳黑子字典 此函数将要打开的文件名和空字典作为参数。它没有返回值,但用值填充字典。关键应该是今年,;数据应该是当年几个月的太阳黑子数据列表。 主要功能是: def main(): sunspot_dict = {} file_str = raw_input("Open what data file: ") keep_going = True while keep_going: try:

可能重复:

我们可以使用一个主要函数:

编写以下两个函数:

初始化字典文件字符串,太阳黑子字典 此函数将要打开的文件名和空字典作为参数。它没有返回值,但用值填充字典。关键应该是今年,;数据应该是当年几个月的太阳黑子数据列表。 主要功能是:

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中IO的更多信息

您需要打开文件,提取其内容,然后填充字典

看看


一个提示是看看这个例子,他们试图用一个文件制作一个字典

我不知道从哪里开始!您需要从某个地方开始,因此,如果您不知道如何使用Python进行文件I/O,请先打开并阅读文件搜索。一旦你遇到一个具体的问题,问一个问题,幸运的话,也许有人能帮上忙。我需要一年成为一年中的关键,价值成为数据,这是一些txt文件:月平均太阳黑子数==============================================================================================================================================================================================年1月2月3月4月6月7日8月9日10月11月12日----------------------------------------------------------------------------------------------------------------------------------------------------------------------1749 58.0 62.6 70.0 55.7 85.0 83.5 94.8 66.3.75.975.5 158.6 85.2 1750 73.3 75.9 89.2 88.3 90.0 100.0 85.4 103.0 91.2 65.7 63.3 75.4首先编写用于读取和写入文件的代码。。。然后你就会明白。。。制作字典并不困难,解析文件数据是。。所以,第一个文件是什么?输入要打开的文件:def init_dictionaryfile?str,sunsspot?dict:line=openfile?str,rU sunsspot?dict={}对于行中的i:sunsspot?dict[i]打印sunsspot?dict这样做。。。data=line.readlines读取文件后。。现在数据包含文件的每一行。。。在数据中执行for循环,并根据需要解析元素。。。