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

JavaScript

frontend.py

JavaScript

backend.py

JavaScript

inventory.csv

JavaScript

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.

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement