tags:

views:

61

answers:

2

I would like to read different files in one directory with the following structure:

#   Mj =  1.60      ff    =   7580.6    gg =  0.8325

I would like to read the numbers from each file and associate every one to a vector. If we assume I have 3 files, I will have 3 components for vector Mj, ... How can I do it in Python?

Thanks for your help.

+1  A: 

I'd use a regular expression to take the line apart:

import re
lineRE = re.compile(r'''
    \#\s*
    Mj\s*=\s*(?P<Mj>[-+0-9eE.]+)\s*
    ff\s*=\s*(?P<ff>[-+0-9eE.]+)\s*
    gg\s*=\s*(?P<gg>[-+0-9eE.]+)
    ''', re.VERBOSE)

for filename in filenames:
    for line in file(filename, 'r'):
        m = lineRE.match(line)
        if not m:
            continue
        Mj = m.group('Mj')
        ff = m.group('ff')
        gg = m.group('gg')
        # Put them in whatever lists you want here.
Mike DeSimone
A: 

Here's a pyparsing solution that might be easier to manage than a regex solution:

text = "#   Mj =  1.60      ff    =   7580.6    gg =  0.8325 "

from pyparsing import Word, nums, Literal

# subexpression for a real number, including conversion to float
realnum = Word(nums+"-+.E").setParseAction(lambda t:float(t[0]))

# overall expression for the full line of data
linepatt = (Literal("#") + "Mj" + "=" + realnum("Mj") +
            "ff" + "=" + realnum("ff") +
            "gg" + "=" + realnum("gg"))

# use '==' to test for matching line pattern
if text == linepatt:
    res = linepatt.parseString(text)

    # dump the matched tokens and all named results
    print res.dump()

    # access the Mj data field
    print res.Mj

    # use results names with string interpolation to print data fields
    print "%(Mj)f %(ff)f %(gg)f" % res

Prints:

['#', 'Mj', '=', 1.6000000000000001, 'ff', '=', 7580.6000000000004, 'gg', '=', 0.83250000000000002]
- Mj: 1.6
- ff: 7580.6
- gg: 0.8325
1.6
1.600000 7580.600000 0.832500
Paul McGuire
Interesting. Now try writing it without `from pyparsing import *`, which is [strongly discouraged.](http://docs.python.org/howto/doanddont.html#from-module-import)
Mike DeSimone
I discourage it too (http://my.safaribooksonline.com/9780596514235/basic_form_of_a_pyparsing_program) but I guess I get hasty in dashing off these simple parsers.
Paul McGuire