hey guys, beginner here. I have written a program that outputs files to .txt’s and am using another to read them and use them. i have used a list to store these values (len(..) gives me 100 for all files). However, whenever i run this:
for w in range(1,20): # i want files file01-file20 excluding file00
for x in range(100):
c=c+1 #counter to keep list position on f=0
exec "f=open('file%02d.txt','r').readlines()"%w #stores data from file00,file01,file02...
f00=open('file00.txt','r').readlines() #same as ^ but from file00
for y in range(100):
xvp=float(f[c].rstrip('\n')) #the error is on this line; the file are stored in vertical order
pvp=float(f00[y].rstrip('\n')) #maybe even this one
#and i do stuff with those values...
I get in line 12,
xvp=float(f[c].rstrip(‘\n’))
IndexError: list index out of range
note: there are 100 numbers stored on separate lines in the .txt’s
please, if there is any way to help you help me, let me know
thanks
You seem to be incrementing
ctwo thousand times (20 times 100 — actually only 1900 times, sincerange(1,20)will not reach the value 20, as you seem to desire in a comment) — so of course you’re going out of range if you use it to index a list of 100! The whole code is rather a mess and I suggest refactoring it radically, to avoidexecand do things the Python way. Assuming Python 2.6 or better (in 2.5, you need afrom __future__ import with_statementat the start of your module):I don’t know if this is the logic you want — coupling every line of
file00.txtwith each line from the 20 other files — but at least this makes it clear which lines are coupled up with which;-). If what you want is to only couple the first line offile00.txtwith the first line from each of the others, then second line with second lines, etc, then addimport itertoolsat the start of your module and change the contents of thewithinto:and so forth.
Note that I’m reading all of
file00.txtin memory once and for all (into thef00list of lines) because apparently you need to loop on those contents more than once, but that’s not needed for the other files.An obvious optimization is to convert
file00.txt‘s lines to floats only once, replacing thef00 =statement withthen use
rvpsdirectly instead of repeating the conversion every time on the strings inf00— for example, in the second version (the one usingitertools.izip):Edit: I see I’ve done a number of tiny enhancements while hardly realizing I was doing so, maybe I’d better explain them;-). No need to pass
'r'when opening a file for reading (can’t hurt, but it’s quite idiomatic to omit it). No need to strip trailing (or for that matter leading) whitespace from a string before callingfloaton it —floathappily skips all such leading and trailing whitespace itself. I did fix what apparently was another bug (you’d never deal withfile20.txt) by fixing the applicablerangetorange(1, 21).The
with open(...) as f:statements do the opening, bind namefto the open file object, and, as soon as the block of statements they control is finished, guarantee that the file is properly closed — it should almost invariably be used in preference to a stand-aloneopen, because ensuring all files are closed ASAP is really very good practice (thewithstatement has many other excellent use cases, but this is the single most frequent one, and the only one that happens to be necessary for this functionality).Looping directly on an open file object
f(provided the file is opened in text mode, as is the default and applies throughout here),for line in f:, provides one after the other the lines off(without ever needing to keep them all in memory at once) and is an extremely popular and good Pythonic idiom.The construct
rvps = [float(line) for line in f], which I use in my recommended optimization, is known as a “list comprehension” and it’s a nicely speedy and compact alternative to a loop that builds a new list.itertools.izip, given a number of iterables, provides a single iterable whose items are tuples made by the items of the other iterables “walked in lockstep”. The built-inzipis similar, but (in Python 2) it builds a list in memory, whichitertools.izipavoids, so it’s good practice to learn to use theitertoolsversion to avoid wasting memory (not really important for small files like the ones you have, but good habits are best learned and “just applied” rather than having to reflect on them every single time — just one one doesn’t start every morning pondering whether one should brush one’s teeth, but just goes and does so as a matter of good habit;-).I’m sure there’s more, but this is what comes to mind off-hand – feel free to ask if I can be of further assistance!