我試圖從PUT_LOG標記中提取(LOG_LEVEL_DEBUG,CAPTIVE_RECVD_SIGCHLD),只要找到PUT_LOG標記,我只想提取其中的信息並將其放入另一個文件中。Python發現特定字符串時從文件中提取數據
請找到的代碼片段從那裏要提取信息...
PUT_LOG(LOG_LEVEL_DEBUG, CAPTIVE_EXECUTE_CMD,1,STRING, cmd);
PUT_LOG(LOG_LEVEL_DEBUG, CAPTIVE_EXECUTE_CMD,1,STRING, cmd);
PUT_LOG(LOG_LEVEL_DEBUG_ERR, CAPTIVE_EXECUTE_CMD_FAILED,1, STRING, cmd);
enter code here
my out put should be like this
LOG_LEVEL_DEBUG
CAPTIVE_EXECUTE_CMD
LOG_LEVEL_DEBUG_ERR
CAPTIVE_EXECUTE_CMD_FAILED
我已經寫了這樣的代碼,你..can修改,並給我exctly代碼
#!/usr/bin/env python
inFile = open("data.c")
outFile = open("result.txt", "w")
buffer = []
keepCurrentSet = True
for line in inFile:
buffer.append(line)
if line.startswith("PUT_LOG"):
#---- starts a new data set
if keepCurrentSet:
outFile.write("".join(buffer))
#now reset our state
keepCurrentSet = False
buffer = []
elif line.startswith("LOG_LEVEL_DEBUG"):
keepCurrentSet = True
inFile.close()
outFile.close()
這將是很好看你有沒有嘗試過的東西。這是所有*「你告訴我你的,我告訴你我的」*在SO :) – root 2013-03-12 06:09:48
你明白了嗎..我的預期結果是什麼 – Manu 2013-03-12 06:24:46