gpt4 book ai didi

python - 使用 Python 查找多个单词并打印下一行

转载 作者:行者123 更新时间:2023-11-28 17:52:59 28 4
gpt4 key购买 nike

我有一个巨大的文本文件。看起来如下

> <Enzymologic: Ki nM 1>
257000

> <Enzymologic: IC50 nM 1>
n/a

> <ITC: Delta_G0 kJ/mole 1>
n/a

> <Enzymologic: Ki nM 1>
5000

> <Enzymologic: EC50/IC50 nM 1>
1000

.....

现在我想创建 python 脚本来查找像 ( > <Enzymologic: Ki nM 1> , > <Enzymologic: EC50/IC50 nM 1> ) 这样的词,并以制表符分隔的格式在每个词的下一行打印如下

> <Enzymologic: Ki nM 1>     > <Enzymologic: EC50/IC50 nM 1>
257000 n/a
5000 1000
....

我试过下面的代码

infile = path of the file
lines = infile.readlines()
infile.close()
searchtxt = "> <Enzymologic: IC50 nM 1>", "> <Enzymologic: Ki nM 1>"
for i, line in enumerate(lines):
if searchtxt in line and i+1 < len(lines):
print lines[i+1]

但它不起作用,任何人都可以建议一些代码......来实现它。

提前致谢

最佳答案

s = '''Enzymologic: Ki nM 1

257000

Enzymologic: IC50 nM 1

n/a

ITC: Delta_G0 kJ/mole 1

n/a

Enzymologic: Ki nM 1

5000

Enzymologic: IC50 nM 1

1000'''
from collections import defaultdict

lines = [x for x in s.splitlines() if x]
keys = lines[::2]
values = lines[1::2]
result = defaultdict(list)
for key, value in zip(keys, values):
result[key].append(value)
print dict(result)

>>> {'ITC: Delta_G0 kJ/mole 1': ['n/a'], 'Enzymologic: Ki nM 1': ['257000', '5000'], 'Enzymologic: IC50 nM 1': ['n/a', '1000']}

然后根据需要格式化输出。

关于python - 使用 Python 查找多个单词并打印下一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6343355/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com