Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9110017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:10:39+00:00 2026-06-17T03:10:39+00:00

I have an input file which is in a Fortran namelist format which I

  • 0

I have an input file which is in a Fortran “namelist” format which I would like to parse with python regular expressions. Easiest way to demonstrate is with a ficticious example:

$VEHICLES
 CARS= 1,
 TRUCKS = 0,
 PLAINS= 0, TRAINS = 0,
 LIB='AUTO.DAT',
C This is a comment
C Data variable spans multiple lines
 DATA=1.2,2.34,3.12,
      4.56E-2,6.78,
$END
$PLOTTING
 PLOT=T,
 PLOT(2)=12,
$END

So the keys can contain regular variable-name characters as well as parenthesis and numbers. The values can be strings, boolean (T, F, .T., .F., TRUE, FALSE, .TRUE., .FALSE. are all possible), integers, floating-point numbers, or comma-separated lists of numbers. Keys are connected to their values with equal signs. Key-Value pairs are separated by commas, but can share a line. Values can span multiple lines for long lists of numbers. Comments are any line beginning with a C. There is generally inconsistent spacing before and after ‘=’ and ‘,’.

I have come up with a working regular expression for parsing the keys and values and getting them into an Ordered Dictionary (need to preserve order of inputs).

Here’s my code so far. I’ve included everything from reading the file to saving to a dictionary for thoroughness.

import re
from collections import OrderedDict

f=open('file.dat','r')
file_str=f.read()

#Compile regex pattern for requested namelist
name='Vehicles'

p_namelist = re.compile(r"\$"+name.upper()+"(.*?)\$END",flags=re.DOTALL|re.MULTILINE)

#Execute regex on file string and get a list of captured tokens
m_namelist = p_namelist.findall(file_str)

#Check for a valid result
if m_namelist:
    #The text of the desired namelist is the first captured token
    namelist=m_namelist[0]

#Split into lines
lines=namelist.splitlines()

#List comprehension which returns the list of lines that do not start with "C"
#Effectively remove comment lines
lines = [item for item in lines if not item.startswith("C")]

#Re-combine now that comment lines are removed
namelist='\n'.join(lines)

#Create key-value parsing regex
p_item = re.compile(r"([^\s,\=]+?)\s*=\s*([^=]+)(?=[\s,][^\s,\=]+\s*\=|$)",flags=re.DOTALL|re.MULTILINE)

#Execute regex
items = p_item.findall(namelist)

#Initialize namelist ordered dictionary
n = OrderedDict()

#Remove undesired characters from value    
for item in items:
    n[item[0]] = item[1].strip(',\r\n ')

My question is whether I’m going about this correctly. I realize there is a ConfigParser library, which I have not yet attempted. My focus here is the regular expression:

([^\s,\=]+?)\s*=\s*([^=]+)(?=[\s,][^\s,\=]+\s*\=|$)

but I went ahead and included the other code for thoroughness and to demonstrate what I’m doing with it. For my Regular Expression, because the values can contain commas, and the key-value pairs are also separated by commas, there is no simple way to isolate the pairs. I chose to use a forward look-ahead to find the next key and “=”. This allows everything between the “=” and the next key to be the value. Finally, because this doesn’t work for the last pair, I threw in “|$” into the forward look-ahead meaning that if another “VALUE=” isn’t found, look for the end of the string. I figured matching the value with [^=]+ followed by a look-ahead was better than trying to match all possible value types.

While writing this question I came up with an alternative Regular Expresson that takes advantage of the fact that numbers are the only value that can be in lists:

 ([^\s,\=]+?)\s*=\s*((?:\s*\d[\d\.\E\+\-]*\s*,){2,}|[^=,]+)

This one matches either a list of 2 or more numbers with (?:\s*\d[\d\.\E\+\-]*\s*,){2,} or anything before the next comma with [^=,].

Are these somewhat messy Regular Expressions the best way to parse a file like this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T03:10:41+00:00Added an answer on June 17, 2026 at 3:10 am

    I would suggest to develop little more sophisticated parser.

    I stumble upon the project on google code hosting that implements very similar parser functionality: Fortran Namelist parser for Python prog/scripts but it was build for little different format.
    I played with it a little and updated it to support structure of the format in your example:

    Please see my version on gist:
    Updated Fortran Namelist parser for python https://gist.github.com/4506282

    I hope this parser will help you with your project.

    Here is example output produced by the script after parsing FORTRAN code example:

    {'PLOTTING': 
        {'par': 
            [OrderedDict([('PLOT', ['T']), ('PLOT(2) =', ['12'])])],
        'raw': ['PLOT=T', 'PLOT(2)=12']},
     'VEHICLES': 
        {'par': 
            [OrderedDict([('TRUCKS', ['0']), ('PLAINS', ['0']), ('TRAINS', ['0']), ('LIB', ['AUTO.DAT']), ('DATA', ['1.2', '2.34', '3.12', '4.56E-2', '6.78'])])],
      'raw': 
                    ['TRUCKS = 0',
                      'PLAINS= 0, TRAINS = 0',
                      "LIB='AUTO.DAT'",
                      'DATA=1.2,2.34,3.12',
                      '4.56E-2,6.78']}}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Respected sir/madam, I have a fortran exe which takes a input file and produces
I have written xml file which contains html tags as element like <component> <input
I need an input file stream which would have a bidirectional iterator/adapter. Unfortunately std::ifstream
i have an input csv file with columns position_Id, Asofdate, etc which has to
I have JavaScript code which copies the value of input file and paste it
I have a php file that contains a form (which contains 2 input boxes
If I have an input file like: US, P1, AgriZone, H, 1000, 1200, 1101,
I have a FORTRAN .exe file which runs and works ok, it will ask
I have one input file which has html tag embedded in xml for example
I have a file which has data like this USDINR12AUGFUT 58 1344396605 627906 2012-08-08

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.