maurobio maurobio - 10 months ago 58
Python Question

Read and parse a file of tokens?

I have a text file containing commands in a simple (hypothetical) command language, as follows:



2 7 9 10 15
25 40 900 2000

1) Each "COMMAND" starts with a special character ('$') and may be followed by a sequence of digits (the "command parameters").

2) Commands without parameters are considered "boolean commands" and assume by default a value of True.

3) There can be many commands with parameters (I call them here "Alpha", "Beta", etc.), but no matter their names, all are followed by one of more lines containing parameters.

4) There may or may not be blank lines between lines contaning commands.

I wrote a function which reads a file containing said commands and parameters and returns only the parameters of a specific command (passed as a function parameter). Here it is:

def get_params(fname, command):
fspecs = open(fname,"r")

params = []
for cline in fspecs:
cline = cline.strip()
if not cline:
continue # Blank line
if cline.startswith('*'):
if command in cline:
params = cline.partition(command)[-1].split()
#else: # Continuation of a command.
# params.append(cline)

if len(params) == 0: # Boolean command, defaults to True
ret_val = True
ret_val = ' '.join(params) # Numeric command, gets parameters
return ret_val

p = get_params('command_file', '$BOOLEAN_COMMAND')
print p # returns True
p = get_params('command_file', '$NUMERIC_COMMAND_ALPHA')
print p # returns 1 3 6 9 10
p = get_params('command_file', '$NUMERIC_COMMAND_BETA')
print p # should return 2 7 9 10 15, but returns True

The above code works when the parameters of a given command are in a single line (immediately after the command token), but fails when the parameters are in subsequent lines (in that case, it just returns 'True' because no parameters are found after the command token). If the 'else' clause is not commented out, it just takes all lines containing parameters of whatever tokens there are up to the end of the file. Actually running the above code will better demonstrate the problem.

What I want is being able to read one specific token (passed to the function) and get only its parameters, no matter if they extend into several lines or how many other tokens there may be in the command file.

Answer Source

As the commands may take more than one line its much easier to NOT split the text-file by newlines. I would suggest splitting by '$' instead.

This example code works:

def get_params(fname, desired_command):
    with open(fname,"r") as f:
        content =
    for element in content.split('$'):
        element = element.replace('\n', ' ').strip()
        if not element:
        if ' ' in element:
            command, result = element.split(' ', 1)
            command, result = element, True
        if desired_command == command or desired_command == '${}'.format(command):
            return result