从基于特定文本的文件中获取数据-Python

时间:2019-04-23 09:06:52

标签: python list file

我在文件中具有以下格式的数据。

"Attach Listener" #7338 daemon prio=9 os_prio=0 tid=0x00007f51c0009000 nid=0x731c waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
    - None

"lettuce-nioEventLoop-9-155" #362 daemon prio=5 os_prio=0 tid=0x00007f515000c800 nid=0x4f7c runnable [0x00007f50da85d000]
   java.lang.Thread.State: RUNNABLE
    at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
    at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)
    at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:79)
    at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
    - locked <0x0000000082af6f50> (a io.netty.channel.nio.SelectedSelectionKeySet)
    - locked <0x0000000082af8050> (a java.util.Collections$UnmodifiableSet)
    - locked <0x0000000082af7f78> (a sun.nio.ch.EPollSelectorImpl)
    at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
    at io.netty.channel.nio.SelectedSelectionKeySetSelector.select(SelectedSelectionKeySetSelector.java:62)
    at io.netty.channel.nio.NioEventLoop.select(NioEventLoop.java:753)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:409)
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858)
    at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:138)
    at java.lang.Thread.run(Thread.java:745)

   Locked ownable synchronizers:
    - None

"lettuce-nioEventLoop-9-154" #360 daemon prio=5 os_prio=0 tid=0x00007f51d00c3800 nid=0x4dd5 runnable [0x00007f50da45b000]
   java.lang.Thread.State: RUNNABLE
    at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
    at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)
    at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:79)
    at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
    - locked <0x0000000082afa8b0> (a io.netty.channel.nio.SelectedSelectionKeySet)
    - locked <0x0000000082afb9b0> (a java.util.Collections$UnmodifiableSet)
    - locked <0x0000000082afb8d8> (a sun.nio.ch.EPollSelectorImpl)
    at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
    at io.netty.channel.nio.SelectedSelectionKeySetSelector.select(SelectedSelectionKeySetSelector.java:62)
    at io.netty.channel.nio.NioEventLoop.select(NioEventLoop.java:753)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:409)
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858)
    at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:138)
    at java.lang.Thread.run(Thread.java:745)

   Locked ownable synchronizers:
    - None

"Attach Listener" #7338 daemon prio=9 os_prio=0 tid=0x00007f51c0009000 nid=0x731c waiting on condition [0x0000000000000000]
   java.lang.Thread.State: WAITING

   Locked ownable synchronizers:
    - None

我需要根据每个堆栈的第一行存储完整的堆栈跟踪。即“ lettuce-nioEventLoop-9-155”#362守护程序prio = 5 os_prio = 0 tid = 0x00007f515000c800 nid = 0x4f7c可运行[0x00007f50da85d000]

我需要收集每个跟踪的第一行,当我将数据与文件中的数据进行比较时,如果匹配,则需要收集该跟踪的完整跟踪。在某些情况下,同一文件中的其他堆栈跟踪的第一行可能是相同的,如果是这种情况,我需要将其附加到先前已收集的相同数据中。

这就是我所做的-

data_methods = []
tdfilename = r"C:\Users\hello\Desktop\trace_test.txt"
with open(tdfilename) as f:
    for line in f:
        method = re.findall(r'"(.*?)]', line)
        fmethod = ''.join(method)
        if fmethod:
            data_methods.append("\""+fmethod+"]") # Adding " and ] at the start and end of the line as per the file content
f.close()

我正在将所有堆栈跟踪的第一行收集到一个列表中。我的想法是将列表数据与文件中的数据进行比较,如果匹配,则需要收集完整的跟踪记录。我坚持要为此逻辑。

我应该使用dict将第一行保存为键,将内容保存为值,因为使用相同数据可以使第一行保存多次吗?

我该如何做到这一点。我这样做是为了减轻我们日常活动中的某些工作。

2 个答案:

答案 0 :(得分:1)

要在映射中创建新的 并将其添加(如果已存在)的defaultdict会很方便。在这里,我会做:

data_methods = collections.defaultdict(list)
tdfilename = r"C:\Users\hello\Desktop\trace_test.txt"
firstpattern = re.compile(r'".*]\s*$')
with open(tdfilename) as f:
    for line in f:
        if firstpattern.match(line)
        cur = data_methods[line.strip()]
    else:
        cur.append(line)

然后,您只需加入值即可,例如,转储结果:

for k, v in data_methods.items():
    print(k)
    print(''.join(v))

答案 1 :(得分:0)

似乎您可以依靠跟踪格式的缩进。这是一个基本版本:

td_filename = 'trace.txt'

exc_dict = {}

with open(td_filename) as f:
    cur_line = None

    for line in f:
        if line.startswith(' ') or line.startswith('\n'):
            if cur_line is not None:
                exc_dict[cur_line].append(line)
        else:
            if line not in exc_dict:
                exc_dict[line] = []
            cur_line = line

for k in exc_dict:
    print(k)
    print(exc_dict[k])
    print('\n')

如果您要分离单个异常并加入字符串,请尝试以下操作:

td_filename = 'trace.txt'

exc_dict = {}

with open(td_filename) as f:
    cur_line = None

    for line in f:
        if line.startswith(' ') or line.startswith('\n'):
            if cur_line is not None:
                if exc_dict[cur_line][-1] is None:
                    exc_dict[cur_line][-1] = ''
                exc_dict[cur_line][-1] += line
        else:
            if line not in exc_dict:
                exc_dict[line] = []

            exc_dict[line].append(None)
            cur_line = line

for k in exc_dict:
    print(k)
    for e in exc_dict[k]:
        print(e)
        print('\n')