Jan-28-2017, 07:03 PM
Hey,
i have a problem with a homework for university. We have to write a class which can use attributes and read a cs. file. I am on it for several days and dont get a right answer :(
As example i get a csv file like
Also they compare it to manual entered data, but you will see it in the code i will paste.
Here is my code so far, that doesnt really work:
['title1', 'title2'], [['l1'], ['l2']]
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-40-82adc121b91e> in <module>()
20 my_table3_parsed = Table(filename = "simple_example.csv")
21
---> 22 assert my_table3_parsed == my_table3 # Teilpunkte, falls immerhin dies funktioniert
23
24 # dritter Test
<ipython-input-39-dad6bc1a92db> in __eq__(self, other)
18 return (self.titles, self.cols) == (other.titles, other.cols)
19 else:
---> 20 return self.parse_csv(self.filename) == other
21
22 def parse_csv(self, filename):
<ipython-input-39-dad6bc1a92db> in __eq__(self, other)
16 def __eq__(self, other):
17 if self.filename == None:
---> 18 return (self.titles, self.cols) == (other.titles, other.cols)
19 else:
20 return self.parse_csv(self.filename) == other
AttributeError: 'list' object has no attribute 'titles'
i have a problem with a homework for university. We have to write a class which can use attributes and read a cs. file. I am on it for several days and dont get a right answer :(
As example i get a csv file like
Time | Temperature ------------------------- 0.0 | 15.2 3.0 | 16.1 6.0 | 14.8 9.0 | 20.2Than it should "return" an object with type "table" like this:
(["Time", "Temperature"], [[0.0, 3.0, 6.0, 9.0], [15.2, 16.1, 14.8, 20.2]])We have to create an attribute "titles" and "cols", Method "__str__(self)" to output the attributes. Mehod "__eq__(self, other)" which returns true if both attributes are equal. Method "parse_csv(self, filename), which reads the given csv file and returns the data as an object, where we have to use csv modul, and the constructor which gets the attributes or the filename and no attributes.
Also they compare it to manual entered data, but you will see it in the code i will paste.
Here is my code so far, that doesnt really work:
import csv class Table: def __init__(self, titles=None, cols=None, filename = None): if filename is None: self.titles = titles self.cols = cols self.filename = None else: self.filename = filename self.parse_csv(filename) def __str__(self): return (str(self.titles) +', '+ str(self.cols)) def __eq__(self, other): if self.filename == None: return (self.titles, self.cols) == (other.titles, other.cols) else: return self.parse_csv(self.filename) == other def parse_csv(self, filename): with open(filename) as f: reader = csv.reader(f) header = next(reader) cols = zip(*reader) return [header, cols]and i check it with this, given in the homework:
my_table1 = Table(["title1", "title2"], [["l1"], ["l2"]]) print(my_table1) assert my_table1.titles[0] == "title1" assert my_table1.titles[1] == "title2" assert my_table1.cols[0] == ["l1"] assert my_table1.cols[1] == ["l2"] my_table2 = Table(["title1", "title2"], [["l1"], ["l2"]]) assert my_table2 == my_table1 titles = ["Time", "Temperature"] filename1 = "simple_example.csv" my_table3 = Table(titles, [[0.0, 3.0, 6.0, 9.0], [15.2, 16.1, 14.8, 20.2]]) my_table3_parsed = Table(filename = "simple_example.csv") assert my_table3_parsed == my_table3This is the output with the error:
['title1', 'title2'], [['l1'], ['l2']]
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-40-82adc121b91e> in <module>()
20 my_table3_parsed = Table(filename = "simple_example.csv")
21
---> 22 assert my_table3_parsed == my_table3 # Teilpunkte, falls immerhin dies funktioniert
23
24 # dritter Test
<ipython-input-39-dad6bc1a92db> in __eq__(self, other)
18 return (self.titles, self.cols) == (other.titles, other.cols)
19 else:
---> 20 return self.parse_csv(self.filename) == other
21
22 def parse_csv(self, filename):
<ipython-input-39-dad6bc1a92db> in __eq__(self, other)
16 def __eq__(self, other):
17 if self.filename == None:
---> 18 return (self.titles, self.cols) == (other.titles, other.cols)
19 else:
20 return self.parse_csv(self.filename) == other
AttributeError: 'list' object has no attribute 'titles'