我有一个包含堆栈跟踪的文件。我的想法是在该文件中搜索某个单词,如果找到该单词,则将内容从该行移至下一个空行。以下是我的代码-
tdfilename = r"C:\Users\Dev\Desktop\dummystacktrace"
tdf = open(tdfilename)
for num, l in enumerate(tdf, 1):
if "java.lang.Thread.State: RUNNABLE" in l:
runnable += 1
if "java.lang.Thread.State: WAITING (on object monitor)" in l:
objmonitorwaiting += 1
linenum = num
tdf.close()
我能够获得找到的字符串的行号(linenum
),这里我要搜索的字符串是java.lang.Thread.State: WAITING (on object monitor)
,但是我如何获取下一个空行的行号从该点(linenum
)开始,即从第2行到第10或111行,并将所有详细信息保存到变量中。
虚拟堆栈跟踪-
"Agent Execution" #10 daemon prio=5 os_prio=0 tid=0x000074563546f83c4c28000 nid=0x66760e in Object.wait() [0x00007f834737776cf000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000800567670b280> (a com.wily.util.adt.BlockingQueue)
at java.lang.Object.wait(Object.java:502)
at com.wily.util.adt.BlockingQueue.interruptableDequeue(BlockingQueue.java:123)
- locked <0x00000000800567670b280> (a com.wily.util.adt.BlockingQueue)
at com.wily.util.task.AsynchExecutionQueue.doTask(AsynchExecutionQueue.java:200)
at com.wily.util.task.ATask$CoreTask.run(ATask.java:132)
at java.lang.Thread.run(Thread.java:745)
Locked ownable synchronizers:
- None
"Thread Monitor Heartbeat Heartbeat" #6 daemon prio=5 os_prio=0 tid=0x0000342523454c4c26000 nid=0x2260d in Object.wait() [0x00007f8789078903777d0000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x000000008057567004b38> (a com.wily.util.heartbeat.IntervalHeartbeat)
at java.lang.Object.wait(Object.java:502)
at com.wily.util.heartbeat.IntervalHeartbeat.waitForBehaviorIfEmpty(IntervalHeartbeat.java:570)
- locked <0x000000008057567004b38> (a com.wily.util.heartbeat.IntervalHeartbeat)
at com.wily.util.heartbeat.IntervalHeartbeat.access$1(IntervalHeartbeat.java:562)
at com.wily.util.heartbeat.IntervalHeartbeat$HeartbeatRunnable.run(IntervalHeartbeat.java:667)
at java.lang.Thread.run(Thread.java:745)
Locked ownable synchronizers:
- None
"Agent Heartbeat" #5 daemon prio=5 os_prio=0 tid=0x00007f834567465c4c24000 nid=0x60c sleeping[0x00007f8757563778d1000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at com.wily.util.heartbeat.IntervalHeartbeat$HeartbeatRunnable.run(IntervalHeartbeat.java:673)
at java.lang.Thread.run(Thread.java:745)
Locked ownable synchronizers:
- None
请澄清。
答案 0 :(得分:0)
在python中使用字符串的固有布尔类型。
tdfilename = r"C:\Users\Dev\Desktop\dummystacktrace"
tdf = open(tdfilename)
endnum = None
for num, l in enumerate(tdf, 1):
if "java.lang.Thread.State: RUNNABLE" in l:
runnable += 1
if "java.lang.Thread.State: WAITING (on object monitor)" in l:
objmonitorwaiting += 1
linenum = num
if not l.strip() and num > linenum and endnum is None:
end_num = num
tdf.close()
答案 1 :(得分:0)
您可以随身保存它们。无需跟踪行号。
appending
标志跟踪您是否处于需要保存的段中。如果您需要每个文件抓取多个节,则结构可能会变得更加复杂,但是基本思想是相同的。
tdfilename = r"C:\Users\Dev\Desktop\dummystacktrace"
tdf = open(tdfilename)
appending = False
lines = []
for num, l in enumerate(tdf, 1):
if appending:
if l.strip():
lines.append(l)
else:
appending = False
if "java.lang.Thread.State: RUNNABLE" in l:
runnable += 1
if "java.lang.Thread.State: WAITING (on object monitor)" in l:
objmonitorwaiting += 1
linenum = num
appending = True
tdf.close()