[Python.NET] Nested Loops
jvm_cop at spamcop.net
Thu Dec 1 17:59:11 CET 2005
I'm confused. If you do (using W G's variable names)
for refSymbol in symbols.readlines():
won't all the lines be printed? Doesn't the "for" loop process once for each item in the array returned by readlines (changing refSymbol each time through the loop), thus accomplishing what was desired?
It would seem right to me to store the result of myfile.readlines before doing the outer loop, so it's not read each time through the loop through the lines of the symbols file.
Isn't there an idiom to remove the trailing \n chars (and to completely remove the "lines" that are only a newline)? I've been Ruby-ing too much to remember the Python idiom for that.
At 06:55 PM 11/30/2005, michael_sweeney at agilent.com wrote
>I like the idea of using a dictionary as a solution to the problem.
>The reason you are not seeing the nested loop run twice is because of the following:
>>>> fd = file("C:\\defragreport.txt")
>['11/29/2005\n', '\n', '10:32:32 PM\n', '\n', 'Drive C: Defrag completed successfully\n', '\n']
>When you call "readlines()" on the open file, it reads the contents of the complete file. Calling it again returns an empty list. You would have to open/read/close the file in each pass. Thus, the dictionary solution saves you from doing this.
>> > -----Original Message-----
>> > From: pythondotnet-bounces at python.org [mailto:pythondotnet-
>> > bounces at python.org] On Behalf Of W G
>> > Sent: Tuesday, November 29, 2005 2:53 PM
>> > To: pythondotnet at python.org
>> > Subject: [Python.NET] Nested Loops
>> > Hello,
>> > The follow code willl read lines of two text files. It supposed to take
>> > the
>> > first line of the first text file and compare it to all the lines of the
>> > second text file, then go to the next line of the first text file and do
>> > the
>> > same and so on.
>> > The problem is that once the inner loop is finished, it never goes in
>> > loop again. Any suggestions?
>> > Thank you,
>> > Wes
>> > The Code:
>> > for refSymbol in symbols.readlines():
>> > for lookupSymbol in myfile.readlines():
>> > showme = lookupSymbol.split('\t')
>> > if showme == refSymbol.strip():
>> > priceNew.write(refSymbol.strip()+" "+showme)
J. Merrill / Analytical Software Corp
More information about the PythonDotNet