简体   繁体   English

使用python从特定行中找到空行的行号

[英]Find the line number of a empty line from a certain line using python

I have a file which contains the stack trace. 我有一个包含堆栈跟踪的文件。 My idea here is to search for a certain word in that file, and if found, get the contents from that line to the next empty line. 我的想法是在该文件中搜索某个单词,如果找到该单词,则将内容从该行移至下一个空行。 Below is my code - 下面是我的代码-

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()

I am able to get the line number ( linenum ) of the string i found, here the string i am searching for is java.lang.Thread.State: WAITING (on object monitor) But how do i get the line number of the next empty line from that point ( linenum ) ie line 2 to line 10 or 111 and save all the details into a variable. 我能够获取找到的字符串的行号( linenum ),在这里我要搜索的字符串是java.lang.Thread.State: WAITING (on object monitor)但是我如何获取下一个的行号从该点( linenum )(即第2行到第10或111行)的空行,并将所有详细信息保存到变量中。

Dummy Stack Trace - 虚拟堆栈跟踪-

"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

Kindly clarify. 请澄清。

Use the inherent boolean nature of a string in python. 在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()

You can just save them off as you go. 您可以随身保存它们。 No need to track line numbers. 无需跟踪行号。

The appending flag keeps track of whether you're in a segment you need to save. appending标志跟踪您是否处于需要保存的段中。 If you need to grab multiple sections per file, the structure might get more complicated, but the basic idea is the same. 如果您需要在每个文件中抓取多个部分,则结构可能会变得更加复杂,但是基本思想是相同的。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM