python - Inputting textfile into list of class objects, then output the list contents -


As part of my existing project, I am trying to load the content of the text object into the list of objects Then the produce of the goods on a cursory look, my instructor said that my code looks ok, but there is no output. I press enter, and ... nothing. This "not loaded" code is also not printing. It only indicates a new input like I did not enter any direction, I do not know that the objects are being loaded properly in the list, and I am thinking that they are not. I am using only the Windows console to run it, so debugging is limited and slower.

I'm new to Python, but this is not Python course, this is algorithm. But it is necessary that our projects will be written in Python, so I am learning that I will go. I have learned the code I have learned in C, C ++, or Java, usually from here and my instructor has given me some useful tutorials. I have searched here, but I think I have a lot of code. is. So, I ask you very clever people ...

What am I missing here? Did I load the file incorrectly in memory? Is the print line wrong? Am I ignoring something else?

Any help would be most appreciated

Update: It's running for good people running below, but now I'm getting the following error: < blockquote>

traceback (most recent call final): file "D: \ CPSC335 \ Project_2 \ project2.py", line 28, main () file "D: \ CPSC335 \ Project_2 \ project2.py", line 24, .append (DebianPackage (* line.split ()) in the main package: TypeError: init () 2 Expected positional arguments are missing: 'vote' and 'size'

It seems that I have data Incorrect CDD input from dragging text file:

  c: \ Python 34> Python D: \ CPSC335 \ Project_2 \ project2.py D: \ CPSC335 \ Project_2 \ packages.txt 5   

filename: packages.txt

  debianutils 128329 90 libgcc1 128327 46 DPKG 128294 2672 PRL-Base 12736 9 1969 Debrec 121503 168 grep 121426 595 gzip 121346 142 Login 121332 980 Corloft ils 121240 6505 bash 121229 1673 Code: project2.py  
  Import system class DebianPackage: def __init __ (self, name, vote) , Size): self.name = name self.votes = votes self.size = size   

code: project2.py (Update based on the big help below )

  Def (I): Take if (Siskargv)! = 3: print ('Error: You must supply exactly three arguments. \ N \ n' + 'Usage: Python 3 & lt; Python source code file & gt; & lt; text file & gt; & lt; n & Gt; & lt; w & gt; 'sys.exit (1) file name = sys.argv [1] n = int (sys.argv [2]) line = open (filename) .readlines () print ( 'Loaded' '+ filename) package = rows in lines []: packages.append for package in package (DebianPackage (* line.split ())): Print (package.name +' + + package.vot + '' + Package.size) If __name__ == "__main__": main ()    

You define a class and a function named function in your module but it is not called it. So after the definition, keep it at the end:

  main ()   

There are other problems in your code. Your constructor name must be:

  def __init __ (...)   

is better to use readlines (): open (file name) .readlines ()

and use lines for loop. So your code will look something like this:

  imported class DebianPackage: def __init __ (self, name, vote, size): self.name = name self.votes = votes self.size = size Def main (): if len (sys.argv)! = 3: print ('error: you must supply exactly two arguments. \ N \ n' + 'Usage: Python 3 & lt; Python source code file & gt; & lt; text file & gt; & lt; n & Gt; & lt; w & gt; 'sys.exit (1) filename = sys.argv [1] n = int (sys.argv [2]) line = open (filename) .readlines () print (' Load '' + Filename) Package = [] line for line: packages.append (DebianPackage (* line.split ())) Package for package: Print (package name + '+ + package.votes +' + Package. Size) main ()    

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -