[Tutor] Reading from files problem
ALAN GAULD
alan.gauld at btinternet.com
Tue Apr 21 09:30:27 CEST 2009
> this worked for me fine.
I'm glad you solved tyour problem.
But you have done so in an extremely inefficient way, both inefficient
in the way it uses computer resources - both CPU and storage, and
inefficient in the way it uses your time - way more code than you need.
If you only need an answer to this specific problem now then thats
not a problem, if you ever intend to write another program it is
worth learning how to improve the code quality. Take the time to
look at the suggestions you were given so that at least in the future
you can avoid making the same mistakes and save yourself some time.
> #Ask user for student ID to lookup student information
> sought_id = raw_input("Please enter a student identifier to look up grades: ")
Perhaps the biggest inefficiency of all is that you reprocess all of the data
every time you run the program. Yet you are taking the trouble to split the
data out into separate files. Why not use those files?
If you checked if the required data already existed you could save
an awful lot of work for your processor and your disk as well as
speed things up for your user. With a small number of students
the latter issue probably isn't a big concern however.
for line in gradesfile:
...
#variable for output to request.dat text file
req_output = student_id + ", " + last_name + ", " + first_name + ", " + time_acc
#Check to see if student id number is in the list of student id numbers
if lines[0] == sought_id:
new_file = open("requests.dat", "w+")
new_file.write(req_output)
Here you write a single file despite processing all of the input data.
print "\n", last_name + ", "+ first_name + ", " + student_id + ", " + major
But here you ignore the files you (may) already have created and
print the data out from scratch again. It works but it doesn't make much sense.
> hope this helps someone.
> I know it's probably not the most efficient way to handle this kind of problem
> but It works nonetheless.
It is almost the most inefficient way to do it.
You can get from New York to San Francisco via Europe but
I don't recommend it.
Alan G
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090421/c3e70a26/attachment.htm>
More information about the Tutor
mailing list