reading file data mixing strings and numbers in python
Question: 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.


Answer: 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.
    

