从文档中批量提取需要的内容到表格中

img

img


根据下面的序列号从上面的数据库里找到蛋白序列

1)文档是什么文档?
2)从四千多条中提取六百多条,是按照什么样的规则?
3)表格操作可以参考openpyxl

得给出样本数据啊


with open("蛋白序列.txt",'r',encoding='utf8') as f:
    temp=[]
    start=0
    end=600
    for i in range(start,end):
        # 假设一行一条,提取前600条,start=0,end-start=600,end=600
        temp.append(f.readline())
    with open('蛋白序列.csv','w+') as E:
        # 将提取的数据写入csv表格中
        for i in temp:
            # csv表格以,作为每个表格分隔符
            # 这里假设文件中数据分割是空格
            #如果是table 则用\t
            E.write(i.replace(' ',','))

如果有样本数据或者模拟数据更好,这样可以针对你的格式来写,写入格式也可以做一下

如果是doc或者txt文档用正则表达式筛选然后写入xls即可,其他文档一般来讲也可以化成这两种类型的

import pandas as pd 
import re

key_file = r'C:\Users\Administrator\Desktop\key.txt'
data_file = r'C:\Users\Administrator\Desktop\test.xlsx'

with open(key_file, 'r', encoding='utf-8') as f:
    con = f.read()
    res = re.findall(r">(.*?)\[", con, re.DOTALL)
    res = [i.strip() for i in res]
    print(res)

pd.set_option("display.max_columns", None)
df = pd.read_excel(data_file, skiprows=2, names=['序列号', '蛋白名称', '信号肽', '位置', '类型', '蛋白序列'])
dd = df.loc[df.序列号.isin(res)]

print(dd)

img

img