Skip to content
Snippets Groups Projects
Commit 17ad00f7 authored by Eric Kooistra's avatar Eric Kooistra
Browse files

Added remove_key_from_dict_file(). Added rename_key_in_dict_file().

parent 43b1bbd6
No related branches found
No related tags found
No related merge requests found
...@@ -195,7 +195,7 @@ class CommonDictFile: ...@@ -195,7 +195,7 @@ class CommonDictFile:
def insert_key_in_dict_file_before_another_key(self, filePathName, key, value, beforeKey): def insert_key_in_dict_file_before_another_key(self, filePathName, key, value, beforeKey):
"""Write a new key = value pair in the filePathName file just before another existing beforeKey.""" """Write a new key = value pair in the filePathName file just before another existing beforeKey."""
# Read dict file into string and insert new key = value pair at insertLineNr # Read dict file into string and insert new key = value pair before beforeKey
dict_string = '' dict_string = ''
with open(filePathName, 'r') as fp: with open(filePathName, 'r') as fp:
for line in fp: for line in fp:
...@@ -207,7 +207,39 @@ class CommonDictFile: ...@@ -207,7 +207,39 @@ class CommonDictFile:
with open(filePathName, 'w') as fp: with open(filePathName, 'w') as fp:
fp.write(dict_string) fp.write(dict_string)
def change_key_in_dict_file(self, filePathName, key, value): def remove_key_from_dict_file(self, filePathName, key):
"""Remove a key and value pair from the dictfile."""
# Read dict file into string and skip the lines of the key = value pair that must be removed
dict_string = ''
with open(filePathName, 'r') as fp:
remove_line = False
for line in fp:
if remove_line==True:
if line.find(self.CDF_SEPARATOR)>=0:
remove_line = False # found next key, which indicates the end of remove key-value pair
if line.find(key)==0:
remove_line = True # found key that has to be removed
if not remove_line:
dict_string += line
# Write string to dict file
with open(filePathName, 'w') as fp:
fp.write(dict_string)
def rename_key_in_dict_file(self, filePathName, old_key, new_key):
"""Write new key name for old_key in the filePathName file."""
# Read dict file into string
with open(filePathName, 'r') as fp:
dict_string = fp.read()
# Rename old_key in dict string
old_key_start = dict_string.find(old_key) # find old_key
if old_key_start>0:
separator_start = dict_string.find(self.CDF_SEPARATOR, old_key_start) # find separator
dict_string = dict_string[0:old_key_start] + new_key + ' ' + dict_string[separator_start:] # replace old key by new key
# Write string to dict file
with open(filePathName, 'w') as fp:
fp.write(dict_string)
def change_key_value_in_dict_file(self, filePathName, key, value):
"""Write new value for key in the filePathName file. The original key = value pair must fit on one line.""" """Write new value for key in the filePathName file. The original key = value pair must fit on one line."""
# Read dict file into string # Read dict file into string
with open(filePathName, 'r') as fp: with open(filePathName, 'r') as fp:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment