1 回答
TA贡献1790条经验 获得超9个赞
PMID您可以使用as 键和AUTHORs 作为值来收集字典中的数据。
假设您从文件开始
from io import StringIO
fo = StringIO(
'''PMID- 12345678
xyz - text (might be multiple lines)
xyz- text (might be multiple lines)
AUTHOR- author1
AUTHOR- author2
PMID- 12345679
xyz - text (might be multiple lines)
xyz- text (might be multiple lines)
AUTHOR- author3
AUTHOR- author4''')
# with open(filename, 'r') as fo:
然后迭代行并填充字典
records = dict()
pmid = None
for line in fo.readlines():
if line.startswith('PMID-'):
pmid = line.split('-')[-1].strip()
records[pmid] = []
elif line.startswith('AUTHOR'):
records[pmid].append(line.split('-')[-1].strip())
创建数据框时,您可以将df = pd.DataFrame(records)每个作者放在一列中或在传递给数据框构造函数之前加入列表
df = pd.DataFrame(
[', '.join(r) for r in records.values()],
index=records.keys()
)
输出
0
12345678 author1, author2
12345679 author3, author4
添加回答
举报