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

基于特定的文本从文件中获取数据-Python,python,list,file,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-15

我在一个文件中有一个以下格式的数据

"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
我正在将所有堆栈跟踪的第一行收集到一个列表中。我的想法是将此列表数据与文件中的数据进行比较,如果匹配,我需要收集完整的跟踪。我一直想弄清楚这件事的逻辑

我是否应该使用
dict
将第一行保存为键,并将内容保存为值,因为第一行可以在同一数据中多次出现


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

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

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

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

“…因为按键可能重复?”。不,他们不能。对不起,我的错。更新了问题。我的意思是,由于堆栈跟踪的第一行可以出现多次,并且可以是seam,所以我需要将数据附加到同一行中。这些数据看起来像一个日志。如果需要处理大量日志,最好使用ELK堆栈,也可以使用python接口。谢谢你的主意。这是一个数据很少的转储文件,不经常生成。我计划用Python本身来实现这一点,因为我的其他代码都是用PythonHi编写的,谢谢,但是它将每一行分割成一个元素,生成更多的数据行,我添加了这些数据行,并选择了字符串的连接处。您好,当然谢谢。。我会尝试一下让你知道..感谢你的帮助.Serge,你提到了
cur
作为一个字符串和一个列表。对吗?同样对于
数据方法。项:
代码行,我看到
类型错误:'builtin\u function\u或\u method'对象不可编辑
,我们应该在这里添加任何内容吗?它应该是
数据方法.items()
(已修复)。对于第一点,
cur
是一个行列表,这就是我使用
join
来连接它的原因。Python通常更擅长向列表中添加行,而不是反复连接到字符串。谢谢Serge!我可以根据我的需要修改这个。
for k, v in data_methods.items():
    print(k)
    print(''.join(v))