So I’m trying to make a list from the contents of my CSV file and I’m unfortunately still very new to Python as well and it’s been a while since the last time I coded, so it’s a steep learning curve right now. What I’m trying to do is create a list from the CSV file’s data for list manipulation.
The commented out print(row) function below list.append(row) works fine if the append row function is commented out; it prints the data and there’s no issue.
import csv
class csv_file:
# Initializes the class
def __init__(self):
self.list = []
# Pulls a file path into the code and reads the content of the file
def read_csv_file(filepath):
# Takes the file path of the CSV file as an argument.
filepath = "*file name*.csv"
# Opens CSV file
with open(filepath, "r") as csv_file:
# Reads the content of the CSV file.
read = csv.reader(csv_file)
for row in read:
list.append(row)
# print(row)
main.py
from read_csv_file import csv_file
def main():
# Create an instance of the csv_file class
cs = csv_file()
cs.list = cs.read_csv_file()
print(cs.list)
if __name__ == "__main__":
main()
list.append()
TypeError: unbound method list.append() needs an argument
Just trying to put the csv file’s contents into a list.
1
import csv
class CSVFile:
def __init__(self):
self.list = []
def read_csv_file(self, filepath):
with open(filepath, "r") as csv_file:
reader = csv.reader(csv_file)
for row in reader:
self.list.append(row)
return self.list
def main():
cs = CSVFile()
filepath = "*file name*.csv"
cs.read_csv_file(filepath)
print(cs.list)
if __name__ == "__main__":
main()
It will solve your problem
3
The variable you want to be using is self.list
: self.list.append(row)
would work.
Instead, you used list
, with list.append(row)
. The variable list
is the type (class) of which self.list
is an instance.
Let us construct a minimal example:
# `list` is a type
list_instance = list()
# `list_instance` is an instance of that type, equal to `[]`.
print(list_instance)
# => []
# Let's invoke the `.append` method on `list_instance`
list_instance.append(1)
print(list_instance)
# => [1]
# However, in Python, that is a syntactic sugar for
list.append(list_instance, 1)
print(list_instance)
# => [1, 1]
# And when you invoke a method with too few elements, there is an error:
list.append(list_instance)
# ... what were you trying to append?
For any class, invoking a method on an instance in Python is merely a syntactic sugar for invoking the method on the class and supplying an extra parameter for the receiver. This is why instance methods are defined with an extra parameter, conventionally named self
:
# pseudocode:
class list:
def append(self, element):
# ...
Note that there are two parameters defined for list.append
: self
and element
. When you wrote list.append(row)
, self
is set to row
, and element
is missing. If you wrote self.list.append(row)
, this would have been equivalent to list.append(self.list, row)
— two parameters, instead of one.
Past this first problem, there are couple of others. For example,
cs.list = cs.read_csv_file()
print(cs.list)
This does not do anything useful. cs.read_csv_file()
will populate cs.list
, but it contains no return
statement, and thus returns the default value, None
. Then you assign this to cs.list
, undoing all the work it did. The effect is the same as if you wrote cs.list = None
, but with extra steps. Instead, just let cs.read_csv_file()
populate cs.list
:
cs.read_csv_file()
print(cs.list)