1

Here is a part of my parser. The problem is that it not reading and printing DeltaE and Intensity maybe someone could help ?

    if (line[1:24] == "Mulliken atomic charges" or
        line[1:22] == "Lowdin Atomic Charges"):
        if not hasattr(self, "atomcharges"):
            self.atomcharges = {}
        ones = inputfile.next()
        charges = []
        nline = inputfile.next()
        while not "Sum of" in nline:
            charges.append(float(nline.split()[2]))
            nline = inputfile.next()
        if "Mulliken" in line:
            self.atomcharges["mulliken"] = charges
        else:
            self.atomcharges["lowdin"] = charges

    if line[0:6] == 'DeltaE':
        while not "DeltaE" in nline:
            deltae = []
            intensity = []
            line=line.strip()
            #then we have a line like: DeltaE =    13.5423 | TDMI^2 = 0.6670E-01, Intensity =  6553.
            self.deltae = float(line.split('|')[0].strip().split('=')[1].strip())
            line = inputfile.next()
            self.intensity = float(line.split('|')[1].strip().split(',')[1].strip().split('=')[1].strip())
            line = inputfile.next()
            print deltae, ',', intensity

Part of output.log file (this output.log is very huge like 15mb)

   Initial state: <0|
   Final state: |1^1>
   DeltaE =    13.5423 | TDMI^2 = 0.6670E-01, Intensity =  6553.    
   ........................................
   Initial state: <0|
   Final state: |2^1>
   DeltaE =    17.9918 | TDMI^2 = 0.2693    , Intensity = 0.2668E+05
   ........................................
   Initial state: <0|
   Final state: |3^1>
   DeltaE =    22.4523 | TDMI^2 = 0.4740E-01, Intensity =  4644.    
   ........................................

I want to print DeltaE and Intensity after using parse method but nothing is working i can get other values but not DeltaE and Intensity:

 >>> mylogfile.parse()
[Gaussian BChla.out INFO] Creating attribute charge: 0
[Gaussian BChla.out INFO] Creating attribute mult: 1
[Gaussian BChla.out INFO] Creating attribute natom: 82
[Gaussian BChla.out INFO] Creating attribute atommasses[]
[Gaussian BChla.out INFO] Creating attribute atomnos[]
[Gaussian BChla.out INFO] Creating attribute vibsyms[]
[Gaussian BChla.out INFO] Creating attribute vibfreqs[]
[Gaussian BChla.out INFO] Creating attribute vibirs[]
[Gaussian BChla.out INFO] Creating attribute vibdisps[]
[Gaussian BChla.out INFO] Creating attribute temperature: 298.15
[Gaussian BChla.out INFO] Creating attribute enthaply: -2225.475525
[Gaussian BChla.out INFO] Creating attribute freeenergy: -2225.601048
[Gaussian BChla.out INFO] Creating attribute grads[]
[Gaussian BChla.out INFO] Creating attribute entropy: 0.000421006204931
[Gaussian BChla.out INFO] Creating attribute atomcoords[]
[Gaussian BChla.out INFO] Creating attribute coreelectrons[]
<cclib.parser.data.ccData object at 0x02FA1890>
>>>
7
  • I'm not a mind reader. Commented Nov 17, 2015 at 17:09
  • Where is the file you are trying to parse? What result are you getting? What result do you expect? Commented Nov 17, 2015 at 17:10
  • 1
    line[0:11] == 'DeltaE' looks incorrect to me, DeltaE is 6 characters but you are extracting 11 from the line to compare it against. Also nline is not modified inside the while not "DeltaE" in nline loop, so this loop will either short circuit or will execute forever. This is a fragile and error prone way to write a 'parser'. Commented Nov 17, 2015 at 17:18
  • 1
    I edited my comment, there's another obvious error. Also you create two lists deltae = [] intensity = [] and print them out, but never assign anything to them. Commented Nov 17, 2015 at 17:22
  • I would rewrite the parser in a more robust way, using for example regular expressions, this is a horrible mess. Commented Nov 17, 2015 at 17:28

1 Answer 1

1

If you want to keep the same approach, I think all you need to do is remove the extra code that you had mistakenly copied from the atomic charge parsing, this works:

    if line[0:6] == 'DeltaE':
        line=line.strip()
        #then we have a line like: DeltaE =    13.5423 | TDMI^2 = 0.6670E-01, Intensity =  6553.
        self.deltae = float(line.split('|')[0].strip().split('=')[1].strip())
        self.intensity = float(line.split('|')[1].strip().split(',')[1].strip().split('=')[1].strip())
        print(self.deltae, self.intensity)

Here's an example of a different approach for parsing that line - specifying a regular expression that matches the structure of the line. You need to add an import re somewhere such as at the top of the file:

        match = re.search(r"DeltaE =\s+(\S+).* Intensity =\s+(\S+)", line)
        if match is not None:
            self.deltae = float(match.group(1))
            self.intensity = float(match.group(2))
            print(self.deltae, self.intensity)

Here's a complete example that tests the code:

import re

class Parser:
    def parseline(self, line):
        match = re.search(r"DeltaE =\s+(\S+).* Intensity =\s+(\S+)", line)
        if match is not None:
            self.deltae = float(match.group(1))
            self.intensity = float(match.group(2))

p = Parser()
p.parseline("DeltaE =    17.9918 | TDMI^2 = 0.2693    , Intensity = 0.2668E+05")
print(p.deltae, p.intensity)

Output:

17.9918 26680.0
Sign up to request clarification or add additional context in comments.

6 Comments

But this is whole new class and now i need somehow to call this in main parser ? Srr i am just newbie in python
I've created a class just so that I can test my example, and use self to have the same structure as your code. I don't know what your class that does the parsing is called. You should be able to replace all of your code after if line[0:6] == 'DeltaE': with the 4 lines begining with match =. You have to add import re somewhere too.
p = Parser() i also have to define my class name ye ?
You already have a class, and your code is in a class method. I created a class Parser just because I can't see that part of your code and I wanted to test my own.
Well then you did something wrong, such as didn't copy the first line of my code. That's why I post a complete working example.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.