Why is my function only splitting the last line in a data file?

Question:

I’m supposed to be writing a function that will read data from a .txt file and build a dictionary. The headers of the txt file will be the keys, and the values under each header will be values assigned to each key. I currently have:

def GetDataDict():
my_dict = {}
with open(MyFileName,'r') as DataFile:
    headers = DataFile.readline().split()
    for line in DataFile:
        valstrings = line.split()
        n = 0 
        while n < len(headers):
            my_dict[headers[n]] = valstrings[n]
            n += 1
print(my_dict)

The .txt file is organized as:

Pints          fluid          Litres
1.750            1            0.571
3.500            2            1.143
5.250            3            1.714
7.000            4            2.286
8.750            5            2.857
10.500            6            3.429
12.250            7            4.000
14.000            8            4.571
15.750            9            5.143
17.500           10            5.714

The current code I have is defining the keys correctly, however it is only reading the very last line of the .txt file for the values.

Asked By: brandon_ducks

||

Answers:

The issue with your current code is that you are overwriting the values of the dictionary for each line in the file. Specifically, in the while loop, you are setting the value of each header key to the corresponding value in valstrings. However, in each iteration of the loop, you are overwriting the previous value for that key.

To fix this, you need to create a new dictionary for each line in the file, and then add that dictionary as a value for a unique key (e.g. a line number) in the final dictionary. Here’s an updated version of your function that should achieve this:

def GetDataDict(MyFileName):
my_dict = {}
with open(MyFileName,'r') as DataFile:
    headers = DataFile.readline().split()
    line_num = 1
    for line in DataFile:
        valstrings = line.split()
        data_dict = {}
        n = 0
        while n < len(headers):
            data_dict[headers[n]] = valstrings[n]
            n += 1
        my_dict[line_num] = data_dict
        line_num += 1
return my_dict

This version of the function creates a new dictionary data_dict for each line in the file, adds the header-value pairs to that dictionary, and then adds the data_dict as a value to the my_dict dictionary, with a unique key based on the line number.

When you call the function with your example file, it should return a dictionary with 10 key-value pairs, where each key is a line number and each value is a dictionary of header-value pairs.

Answered By: Cat Tien
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.