I have a txt file which contains a lot of information. In my script I open this file and remove unnecessary stuff by using in-built functions and regex, which is done in a for-loop.
Consider the txt file looking like this:
Monkey OFF OFF
Elephant ON ON
and so on...
My script:
x = 'XXX'
y = 'YYY'
value = ''
name = ''
func_file, functionality_file_context = open_func_file(args)
for fline in functionality_file_context:
if fline.strip(): # ignore empty lines or lines with only whitespace
commentregex = re.compile('^[^#]') # ignore lines that start with comment (#)
if (commentregex.match(fline)):
# replace tabs with whitespace
replacetab = re.compile(r'\s+')
fline = replacetab.sub(' ', fline)
function_line = fline.split(' ', 2)
if len(function_line) != 3:
exit("Something wrong with this line: " + fline.strip() + "\" in: " + func_file)
name = x + function_line[0].strip() + y
value = func_override_parameter_value(fline, func_file, function_line,value)
print name + value
myDict = {
"Bike": 0,
"Car": 2,
name: value
}
another_function(args.path, myDict)
The func_override_parameter_value checks what value should be given if for example OFF OFF is matched etc. For example OFF OFF = 1, and ON ON = 2
By doing a print (the print name + value) I get all the names with the correct assigned value.
The open_func_file function:
def open_func_file(args):
functionality_file_context = ''
func_file = ''
for func_file in args.functionality_file:
functionality_file_context = open(func_file).readlines()
return func_file, functionality_file_context
Now to the point:
I have a dict after this for-loop run which already contains some keys and values, for example:
myDict = {
"Bike": 0,
"Car": 2,
}
I want to add new keys and values from the file I just "parsed". I tried calling name and value inside the dict:
myDict = {
"Bike": 0,
"Car": 2,
name: value
}
The end result should be:
myDict = {
"Bike": 0,
"Car": 2,
"XXXMonkeyYYY": 1,
"XXXElephantYYY": 2
}
But this just gives me "Elephant": 2 and skips Monkey. Any idea how to solve this? The order does not matter.
Made an Edit
myDictin your code?myDictif you don't even show the code that handles it?