首页 > 解决方案 > 在 Python 中从文件中的两个匹配项之间提取数据

问题描述

Table 1 Qualitative composition of SERQUEL 25 mg tablets 
Ingredient  Function    Reference to standards
Tablet core     
Quetiapine fumarate Drug substance  This document
Povidone    Binder  USP
Dibasic calcium phosphate dihydrate Diluent USP
Microcrystalline cellulose  Diluent USNF
Sodium starch glycolate Disintegrant    USNF
Lactose monohydrate Diluent USNF
Magnesium stearate  Lubricant   USNF
Purified water  Granulating vehicle USP

以上表示来自 .txt 文件的示例数据:

我有一个要匹配的物质名称列表。(csv 文件中的物质示例是否'Quetiapine fumarate', 'Povidone', 'Magnesium stearate' etc etc.) 存在于 csv 文件中。我想迭代文本文件的每一行并创建从一种物质到另一种物质的组. 示例输出:

['Quetiapine fumarate   Drug substance  This document'],
['Povidone  Binder  USP'],
['Lactose monohydrate   Diluent USNF'],
['Magnesium stearate    Lubricant   USNF']

鉴于“富马酸喹硫平”、“聚维酮”、“乳糖一水合物”、“硬脂酸镁”存在于我的 csv 物质列表中。

我如何在 Python 中做到这一点

标签: pythonregexloopscsvstring-matching

解决方案


您可以使用 readlines 命令

file = open(FOLDER_NAME + '/' + name, 'r', encoding='utf-8') data = file.readlines() for line in data: words=line.split() print(words)

您可以搜索“从python中的文件中读取”

https://www.w3schools.com/python/python_file_open.asp


推荐阅读