次のようなファイルがあります
sys.test1.username = user1
sys.test1.pwd = 1234
sys.test2.username = user2
sys.test2.pwd = 1234
sys.test1.pwd の pwd を sys.test1.pwd = 4321 に変更したい
ファイルを読む
with open (tempfile, 'r') as tempFile:
return self.parse_cfg (self, tempFile.readlines ())
これは sys.test1.pwd を検索して値を取得しています。
def parse_cfg (self, lines):
""" Parse ubnt style configuration into a dict"""
ret_dict = {}
for line in lines:
line = line.strip () # remove new lines
if not line: continue # skip empty lines
key, value = line.split ('=') # key = value
print "key %s" %key
if key == 'sys.test1.pwd':
key = key.strip ()
value = value.strip ()
# logic to parse mainkey.subkey.subkey structure into a dict
keys = key.split ('.')
tempo = ret_dict
for each in keys[:-1]:
tempo.setdefault (each, {})
tempo = tempo[each]
tempo[keys[-1]] = value
break
return ret_dict
しかし、sys.test1.pwd=4321 をファイルに書き込む方法がわかりません。私を助けてください