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

2024-09-27 01:30:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个文件中有一个如下格式的数据。你知道吗

"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

我需要根据每个堆栈的第一行存储完整的堆栈跟踪。i、 e.“莴苣-nioEventLoop-9-155”#362守护程序prio=5 os\u prio=0 tid=0x00007f51500c800 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将第一行保存为键,将内容保存为值,因为第一行可以在同一数据中多次出现?你知道吗

我怎样才能做到这一点。我这样做是为了减轻我们日常活动中的一些工作。你知道吗


Tags: 数据runiolangchanneljavachthread
2条回答

当您想在映射中创建一个新的东西并在它已经存在的情况下添加到它时,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))

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

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

相关问题 更多 >

    热门问题