Create a function that opens a file and creates a dictionary

后端 未结 2 1311
没有蜡笔的小新
没有蜡笔的小新 2021-01-27 14:53

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

2条回答
  •  长发绾君心
    2021-01-27 15:17

    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.

提交回复
热议问题