[Tutor] ideas on how to process a file

Weidner, Ronald RWeidner at ea.com
Fri Apr 10 19:48:53 CEST 2009


In my last e-mail I think I suggested making an Item object.  This time I'll suggest an ItemCollection object.  Now you can have an Add(self, item) that could validate.  This could be some rather simple loop and test logic.

--
Ronald Weidner


From: tutor-bounces+rweidner=ea.com at python.org [mailto:tutor-bounces+rweidner=ea.com at python.org] On Behalf Of Spencer Parker
Sent: Friday, April 10, 2009 1:00 PM
To: Kent Johnson
Cc: Alan Gauld; tutor at python.org
Subject: Re: [Tutor] ideas on how to process a file

The question is now...what do I do to find duplicate entries in the text file I am reading.  I just want to filter them out.  There are a ton of duplicate entries in there.
On Fri, Apr 10, 2009 at 10:43 AM, Spencer Parker <inthefridge at gmail.com<mailto:inthefridge at gmail.com>> wrote:
Oh...nice...this makes things much easier than what I had before.

I mainly used writelines because I couldn't figure out why it was only writing one line.  Then I did and never took out the writelines...I just did and it works just fine for the most part.

Thanks again for the help.

On Fri, Apr 10, 2009 at 10:18 AM, Kent Johnson <kent37 at tds.net<mailto:kent37 at tds.net>> wrote:
On Fri, Apr 10, 2009 at 12:04 PM, Spencer Parker <inthefridge at gmail.com<mailto:inthefridge at gmail.com>> wrote:
>
> This is my code:
> http://pastebin.com/m11053edf
I guess you have something like this now:

for line in text_file.readlines():
       if line.find('FULLNAME')>=0:
               write_file.writelines(line)

This can be better written as

for line in text_file: # No need for readlines(), a file is iterable
 if 'FULLNAME' in line:
   write_file.write(line)  # writelines() is for writing multiple lines at once

Kent


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090410/06498049/attachment-0001.htm>


More information about the Tutor mailing list