Sorry, I missed the last part of the message This makes a more generic parser (comment/uncomment the corresponding "# 1" or "# 2" code based on whether a new block is found by a first line containing "NSTEP" or a last line containing "EWALD"). This > yields a dictionary for each item in the input file. You can pull out whichever value(s) you want to manipulate.
I recognize now that I did not define "block" correctly. The EWALD line, although separated by a blank line, makes part of the block. The layout of all other blocks in the output file is the same. francesco ---------- Forwarded message ---------- From: Francesco Pietra <francesco.pie...@accademialucchese.it> Date: Mon, Jun 8, 2009 at 11:55 PM Subject: Re: Extract value and average To: Tim Chase <python.l...@tim.thechases.com> On Mon, Jun 8, 2009 at 8:54 PM, Tim Chase<python.l...@tim.thechases.com> wrote: >> Of the various suggestions, of which I am most grateful, I >> first tried this one. I probably fixed a couple of indent >> problems, but I don't understand the last one: > > Since Python considers whitespace valueable, indentation needs to match. > When I transfer code into email, I tend to use two-space indentation so > lines don't wrap (in my regular coding, I tend to use hard-tabs with a > tabstop-du-jour of either 2/3/4 spaces-per-tab) and indent it one level to > offset it from the email body text. > > If you use Vim, you can copy/paste my emailed code and simply use > > :%s/ /\t/g > :%s/^\t > > to expand the two-space indents to actual tabs and then the second one > strips off the leading indentation. That done, previous code (for DIHED only) works fine with both the example file.txt france...@deb32:~/tmp$ python tim.py Total: 4660.165 Count: 1 Average: 4660.165 and a file of 102 instances of DIHED: france...@deb32:~/tmp1$ python tim.simple.edited.prod1.py Total: 465628.4416 Count: 102 Average: 4564.98472157 france...@deb32:~/tmp1$ Incidentally, that solves my current problems. Thanks indeed for your generous help. ==================== The more complex parser also worked fine with file.txt: france...@deb32:~/tmp$ python tim.py Total: 4660.165 Count: 1 Average: 4660.165 while (replacing in the parser 'file.txt' with 'prod1.out') with the 102 count file above it reported: france...@deb32:~/tmp$ python tim.prod1.py Traceback (most recent call last): File "tim.prod1.py", line 27, in <module> for thing in builder(): File "tim.prod1.py", line 15, in builder for k,v in pair_re.findall(line) File "tim.prod1.py", line 15, in <genexpr> for k,v in pair_re.findall(line) ValueError: invalid literal for float(): E I did the filename replacement with vim, though it is likely that I did some other mistake. If not, and you are interested in the prod1.out file, I can send it (82 kB). francesco > > From the code you sent, it looks like whitespace didn't get copied into your > script file correctly or your un-indentation didn't take place on every line > (the last 4 lines look like they have an extra space). Once you fix the > whitespace, it should run. For convenience, I've repasted below with > 4-spaces-per-tab and no offsetting indentation. > > -tkc > > ---------------------------- > import re > find_dihed_re = re.compile(r'\bDIHED\s*=\s*([.-e\d]+)', re.I) > total = count = 0 > for line in file('file.txt'): > m = find_dihed_re.search(line) > if m: > str_value = m.group(1) > try: > f = float(str_value) > total += f > count += 1 > except: > print "Not a float: %s" % str_value > print "Total:", total > print "Count:", count > if count: > print "Average:", total/count > > > > > > -- http://mail.python.org/mailman/listinfo/python-list