Note that there are some explanatory texts on larger screens.

plurals
  1. POtables & functions - Pythonic code
    text
    copied!<p>First, I want to note that this is a school assignment <strong>but</strong> I've tried to code it for a long time (for me... I'm such a bad coder). So please, don't think that I want you to do my assigments, not at all!!! I just want your help to improve this code and find what's wrong with it. Hope you didn't get te wrong impression.</p> <p>The explanation about the code is at the end of this post, here's my code:</p> <pre><code>class Table: def __init__(self,path,sep): try: f = open(path, "r") read_file = f.read() f.close() except: print "ERROR" return split_file = read_file.split["\n"] for i in range (len(split_file)): split_file[i] = split_file[i]/split(sep) split_file[0].remove(split_file[0][0]) self.rows = [] self.rows_names = [] for row in split_file[1:-1]: self.rows.append(row[1:]) for get in split_file[1:-1]: self.rows_names.append(get[0]) self.cols.names = split_file[0] for i in range (len(self.cols_names)): self.cols.appens([]) try: for place in self.rows: for i in range(len(place)): self.cols[i].append(place[i]) except: raise ValueError rows_check = [] cols_check = [] for row in self.cols_names: if row in rows_check: raise ValueError else: rows_check.append(row) for cols in self.cols_name: if col in cols_check: raise ValueError else: cols_check.append(col) for number in self.cols[1:]: if number != len(self.cols[0]): raise ValueError </code></pre> <ul> <li><p>I need to write a ctor (which is the function __ init__) that receives as input (in addition to self) ".txt" file name and a string sep. The ctor reads the table and save its data. Each line in the input file (again, it's a simple txt file) contains data which are separated by the string sep.</p></li> <li><p>Opening the txt file will be in a "try-except" form. In case of failure (if there's an Error by opening the file, the code in the "except" part will run and print "There's an Error".</p></li> <li><p>If there's no failure by opening the file text, then the ctor (the function) will use exception of type of ValueError if one of the next cases is true:</p></li> <li><p>There're two columns with the same name</p></li> <li><p>There're two rows same name</p></li> <li><p>There's a row that has a different number of columns than the number of columns of the first row (the number of columns in a row is the size of the list is created after the split function call).</p></li> </ul> <p>notes:</p> <p>***the txt file is a regular one... like the Microsoft notped... ".txt"... nothing special</p> <p>***the txt file contain text which is form as a table. for example:</p> <pre><code>Name,Gender,Age,Salary,Height Menny, M, 1, 1, 1 James, J, 2, 2, 2 Sami, S, 3, 3, 3 </code></pre> <p>I don't know what is the problem of this code, I have some errors while running it and I would like to make this code more <strong>pythonic</strong> but without changing the form of "<em>init</em>".</p> <p>Can someone help me please? I'm kinda lost. Thanks a lot.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload