I have a weird problem when trying create a string when using a dictionary value. Basically, I have a function that opens a file, reads a line, and stores the values it finds in that line in a dictionary. Then, it sends those values to an external program. Here is the code:
def createLandscapes(file):
landscapeParameters = {'FILE': "NULL",
'N': "NULL",
'K': "NULL",
'NUM': "100"}
for line in file:
if line == "END LANDSCAPES\n":
break
else:
parameters = line.replace(" ", '').split(",")
for parameter in parameters:
parameter = parameter.split("=")
if parameter[0] not in landscapeParameters:
malformedFile()
landscapeParameters[parameter[0]] = parameter[1]
for key in landscapeParameters:
if landscapeParameters[key] == "NULL":
malformedFile()
# This print statment is for diagnostic purposes
print("./generateScoreTables {} {} {} {}".format(landscapeParameters['FILE'],
landscapeParameters['N'],
landscapeParameters['K'],
landscapeParameters['NUM']))
os.system("./generateScoreTables {} {} {} {}".format(landscapeParameters['FILE'],
landscapeParameters['N'],
landscapeParameters['K'],
landscapeParameters['NUM']))
To make this very clear, the function looks for a series of parameter inputs on a single, comma separated line, in the form of
FILE=example, N=20, K=5, NUM=100
It takes those inputs and overrides the default inputs (if specified) in landscapeParameters, and uses the values in landscapeParameters to call an external program.
The strange this is that the string formatting doesn't seem to work correctly when I use the default parameters in landscapeParameters. What I mean by this is that if the function reads the line:
FILE=example, N=20, K=5, NUM=100
Everything works correctly, and the print statement prints:
./generateScoreTables example 20 5 100
However, if the function reads the line:
FILE=example, N=20, K=5
Where I've left NUM out to use the default parameter, I get the following output instead:
./generateScoreTables testland1 15
0 100
Segmentation fault
sh: 2: 0: not found
It appears that format is not formatting this string correctly, but I don't understand why. Does anyone have any insight into this?