我有一個文件名爲mcelog.conf
,我正在讀取我的代碼中的這個文件。該文件的內容是從python的文件中讀取一行
no-syslog = yes # (or no to disable)
logfile = /tmp/logfile
計劃將讀取mcelog.conf
文件,將檢查no-syslog
標籤,如果no-syslog = yes
然後程序必須檢查標籤logfile
和將讀取logfile
標籤。任何人都可以讓我知道我怎麼可以得到價值/tmp/logfile
with open('/etc/mcelog/mcelog.conf', 'r+') as fp:
for line in fp:
if re.search("no-syslog =", line) and re.search("= no", line):
memoryErrors = readLogFile("/var/log/messages")
mcelogPathFound = true
break
elif re.search("no-syslog =", line) and re.search("= yes", line):
continue
elif re.search("logfile =", line):
memoryErrors = readLogFile(line) # Here I want to pass the value "/tmp/logfile" but currently "logfile = /tmp/logfile" is getting passed
mcelogPathFound = true
break
fp.close()
您是否嘗試過正則表達式組?甚至在等號上分裂,並且基本上創造了一個鍵值的字典? –
不,我還沒有嘗試正則表達式組,如果我打算用'='符號拆分每一行,那麼有一個機會,如果文件中存在一些註釋,則會出現異常。 @ cricket-007 –
爲什麼?你已經評論了兩個答案,告訴你同樣的事情 –