Skip to content
Advertisement

Read, Split and Append csv to lists in python

Problem I need to build a backend function that will read the data within a csv file, split the values and append them to several lists. The program for adding/removing/displaying values form the lists is working but now I need to add file I/O.

I have the file inventory.csv and have a function load_file_data() which is meant to read, split and then insert the data into three different lists which were initialised as empty lists via the front end.

As this is for a course I have some requirements being that i can’t use the csv reader within Python, it has to be done the “long way”. I also need to ensure that when I write the content of the lists back to the file that it is not appending but instead dumping the entire contents back into the file (wipe and rewrite).

I can’t figure out what is going wrong here as I have initilised the list “colours” along with the other lists using a function that returns and empty list. How is it not defined?

Error Message

Traceback & Error
    Traceback (most recent call last):
      File "frontend.py", line 71, in <module>
        main()
      File "frontend.py", line 12, in main
        backend.load_file_data()
      File "backend.py", line 81, in load_file_data
        add_record(colours, colour)
    NameError: name 'colours' is not defined

frontend.py

def main():
    colours = backend.init_data_structure()
    categories = backend.init_data_structure()
    prices = backend.init_data_structure()
    backend.load_file_data()

backend.py

    def init_data_structure():
        return []
    
    def add_record(list_name, data):
        list_name.append(data)
    
    def load_file_data():
        from_file = open("inventory.csv", "r")
        line = from_file.readline()
        while line != "":
            line = line.strip()
            fields = line.split(",")
            colour = fields[0]
            category = int(fields[1])
            price = float(fields[2])
            add_record(colours, colour)
            add_record(categories, category)
            add_record(prices, price)
        from_file.close()

def save_to_file(list1, list2, list3):
    output = ""
    l = list_length(list1)
    i = 0
    to_file = open("inventory.csv", "w")
    while i < l:
        to_file.write(get_value_at_index(list1, i) + "," + str(get_value_at_index(list2, i)) + "," + str(format(get_value_at_index(list3, i), ".2f")) + "n")
        i += 1
    to_file.close()
    return output

inventory.csv

Red,1,258.47
Red,1,309.08
Brown,2,456.56
Grey,2,317.43
Yellow,2,355.9
Grey,3,379.39
Green,3,477.49
Rainbox,4,302.05
Grey,4,296.31
Rainbox,2,234.33
Dark Green,2,216.11
Dark Blue,2,266.98
Blue,2,433.44
Purple,1,382.75
Purple,1,154.9
Blue,2,201.95
Purple,4,410.38
Rainbox,4,301.7
Rainbox,4,146.83
Grey,2,248.78
Yellow,3,252.34
Burnt Orange,3,209.41
Rainbox,3,259.92
Cream,3,388.13
Orange,4,251.14
Blue,1,194.5
Blue,1,270.09
Yellow,3,308.93
Blue,3,110.78
Cream,3,187.75
Burnt Orange,3,356.41
Grey,3,133.26
Cream,3,238.03
Grey,2,356.02
Burnt Orange,2,71.43
Purple,1,121
Dark Blue,3,320
Cream,4,105.62
Blue,4,279.6
Cream,4,116.13
Rainbox,4,197.68
Grey,4,329.24
Grey,4,264.09
Grey,4,91.99
Purple,4,343.96
Brown,2,353.41
Yellow,3,357.22
Orange,2,155.29
Dark Blue,3,147.6

Advertisement

Answer

I believe I have it working based on help from Zach Young (i.e. colours is not visible within the scope of the load_file_data() function. I have instead passed in the three empty lists to the function as arguments from the frontend and rewritten the function as below.

def load_file_data(list1, list2, list3):
    from_file = open("inventory.csv", "r")
    line = from_file.readline()

    while line != "":
        line = line.strip()
        fields = line.split(",")
        column1 = fields[0]
        column2 = int(fields[1])
        column3 = float(fields[2])

        add_record(list1, column1)
        add_record(list2, column2)
        add_record(list3, column3)

        line =  from_file.readline()
        
    from_file.close()
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement