[Tutor] Nested loop of I/O tasks

wesley chun wescpy at gmail.com
Wed Nov 25 08:04:56 CET 2009


On Tue, Nov 24, 2009 at 2:42 PM, Bo Li <boli1285 at gmail.com> wrote:
>
> I am new to Python and having questions about its usage. Currently I have to read two .csv files INCT and INMRI which are similar to this:
> [...]
> I was a MATLAB user and am really confused by what happens with me. I wish someone could help me with this intro problem and probably indicate a convenient way for pattern matching. Thanks!


greetings and welcome to Python!

the problem you are experiencing is due to the fact that you do not
read in and cache your data first. you are iterating over the data in
both files once, which is what enables your first pass to work.

however, on the second pass, INMRI does not return any more data
because you have already exhausted all lines of the file on the first
pass. if you intend on reiterating over the file, then you must read
in all of the data first and just use that data structure rather than
the actual file as you have.

hope this helps!
--wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
   http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com


More information about the Tutor mailing list