Frequently, there’s a need to extract numerical values from text files located at specific paths. Beginners often resort to manual copying or command-line tools like grep
for extraction, followed by manual copying for further processing. However, Python provides a more seamless way to automate this process.
The following Python code demonstrates how to extract a numerical value from a text file by searching for a specific pattern within the file and retrieving a value at a given position on the matching line.
Example:
Suppose you have a file named outfile
with the following line somewhere in the file:
| Total energy of the DFT / Hartree-Fock s.c.f. calculation : -1103.000899305 eV
The following code will extract the numerical value -1103.000899305
and assign it to a variable, which can be used for further processing.
import re
def extract_val(file_path, pattern, position):
file = open(file_path, "r")
iline=0
for line in file:
if re.search(pattern, line):
iline=iline+1
eline=line
val=float( eline.split()[position] )
#== If no matching string is found, print ERROR
if (iline < 1):
print('ERROR: Can\'t find <',pattern,'> in ',file_path)
val=0
file.close()
return val
== Specify the file path, pattern (a part of the line) and the position of the value in the line
file_path='/Users/rr/Desktop/outfile'.
pattern='| Total energy of the DFT '
position=11
== Call the function
energy=extract_val(file_path, pattern, position)
print(energy)