{"id":110,"date":"2024-01-05T04:00:57","date_gmt":"2024-01-05T04:00:57","guid":{"rendered":"https:\/\/www.tifrh.res.in\/~ramakrishnan\/?p=110"},"modified":"2024-01-05T04:03:10","modified_gmt":"2024-01-05T04:03:10","slug":"extract-a-value-from-a-file-using-python","status":"publish","type":"post","link":"https:\/\/www.tifrh.res.in\/~ramakrishnan\/?p=110","title":{"rendered":"Extract a value from a file using Python"},"content":{"rendered":"\n<p>Frequently, there&#8217;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 <code>grep<\/code> for extraction, followed by manual copying for further processing. However, Python provides a more seamless way to automate this process.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Example:<\/strong><\/h2>\n\n\n\n<p>Suppose you have a file named <code>outfile<\/code> with the following line somewhere in the file: <\/p>\n\n\n\n<p><code>| Total energy of the DFT \/ Hartree-Fock s.c.f. calculation : -1103.000899305 eV<\/code><\/p>\n\n\n\n<p>The following code will extract the numerical value <code>-1103.000899305<\/code> and assign it to a variable, which can be used for further processing.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import re\n\ndef extract_val(file_path, pattern, position):\n\nfile = open(file_path, \"r\")\n\niline=0\nfor line in file:\n    if re.search(pattern, line):\n        iline=iline+1\n        eline=line\n        val=float( eline.split()&#91;position] )\n#== If no matching string is found, print ERROR\nif (iline &lt; 1):\n    print('ERROR: Can\\'t find &lt;',pattern,'> in ',file_path)\n    val=0\nfile.close()\n\nreturn val\n\n== Specify the file path, pattern (a part of the line) and the position of the value in the line\n\nfile_path='\/Users\/rr\/Desktop\/outfile'.\npattern='| Total energy of the DFT '\nposition=11\n\n== Call the function\n\nenergy=extract_val(file_path, pattern, position)\n\nprint(energy)<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><\/h1>\n","protected":false},"excerpt":{"rendered":"<p>Frequently, there&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"off","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[10,5,11],"tags":[],"class_list":["post-110","post","type-post","status-publish","format-standard","hentry","category-codemanagement","category-programmerlife","category-python"],"_links":{"self":[{"href":"https:\/\/www.tifrh.res.in\/~ramakrishnan\/index.php?rest_route=\/wp\/v2\/posts\/110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tifrh.res.in\/~ramakrishnan\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tifrh.res.in\/~ramakrishnan\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tifrh.res.in\/~ramakrishnan\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tifrh.res.in\/~ramakrishnan\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=110"}],"version-history":[{"count":1,"href":"https:\/\/www.tifrh.res.in\/~ramakrishnan\/index.php?rest_route=\/wp\/v2\/posts\/110\/revisions"}],"predecessor-version":[{"id":111,"href":"https:\/\/www.tifrh.res.in\/~ramakrishnan\/index.php?rest_route=\/wp\/v2\/posts\/110\/revisions\/111"}],"wp:attachment":[{"href":"https:\/\/www.tifrh.res.in\/~ramakrishnan\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tifrh.res.in\/~ramakrishnan\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tifrh.res.in\/~ramakrishnan\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}