Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何提取文本文件中两个匹配项(来自csv)之间的数据? 富马酸奎硫平原料药本文件 聚维酮粘合剂USP 此行不包含任何药品名称。 csv中不存在二元磷酸钙二水合物稀释剂USP 一水乳糖稀释剂USNF 硬脂酸镁润滑剂USNF_Python_Regex_Python 3.x_Loops_Pattern Matching - Fatal编程技术网

Python 如何提取文本文件中两个匹配项(来自csv)之间的数据? 富马酸奎硫平原料药本文件 聚维酮粘合剂USP 此行不包含任何药品名称。 csv中不存在二元磷酸钙二水合物稀释剂USP 一水乳糖稀释剂USNF 硬脂酸镁润滑剂USNF

Python 如何提取文本文件中两个匹配项(来自csv)之间的数据? 富马酸奎硫平原料药本文件 聚维酮粘合剂USP 此行不包含任何药品名称。 csv中不存在二元磷酸钙二水合物稀释剂USP 一水乳糖稀释剂USNF 硬脂酸镁润滑剂USNF,python,regex,python-3.x,loops,pattern-matching,Python,Regex,Python 3.x,Loops,Pattern Matching,上面表示.txt文件中的示例数据: 我有一个药物名称列表,我希望在.txt文件中匹配该列表,并提取两种药物之间存在的所有数据。(csv文件中的药物示例为“富马酸奎硫平”、“聚维酮”、“硬脂酸镁”、“一水乳糖”等)。 substancecopy.csv是包含我在下面代码中使用的所有药物列表的文件 我想迭代文本文件的每一行,并创建从一种药物到另一种药物的组 示例输出: ['Quetiapine fumarate Drug substance This document'], ['Povidon

上面表示.txt文件中的示例数据:

我有一个药物名称列表,我希望在.txt文件中匹配该列表,并提取两种药物之间存在的所有数据。(csv文件中的药物示例为
“富马酸奎硫平”、“聚维酮”、“硬脂酸镁”、“一水乳糖”等)。

substancecopy.csv是包含我在下面代码中使用的所有药物列表的文件

我想迭代文本文件的每一行,并创建从一种药物到另一种药物的组

示例输出:

['Quetiapine fumarate   Drug substance  This document'],
['Povidone  Binder  USP'],
['Lactose monohydrate   Diluent USNF'],
['Magnesium stearate    Lubricant   USNF']
鉴于
“富马酸奎硫平”
“聚维酮”
“一水乳糖”
“硬脂酸镁”
存在于我的csv物质清单中

有人能帮我用Python做同样的事情吗

迄今为止:

import re
import pandas as pd
import csv
import os
file = open(r'C:\Users\substancecopy.csv', 'r')
oo=csv.reader(file)
allsub = []
for line in oo:
    allsub.append(line)

flat_list = [item for sublist in allsub for item in sublist]    


def extract(filename):
    file=open(filename,encoding='utf-8')
    file=file.read()

    n=[]
    for x in flat_list:
        my_regex = r"^\s?" + re.escape(x)
        #my_regex_new = r"\b" + re.escape(my_regex) + r"\b"
        if re.search(my_regex,file,flags=re.IGNORECASE|re.MULTILINE):
            n.append(x)


    n.sort()
    return n

我需要捕获从一种药物到另一种药物的所有文本,如示例输出所示,这段代码不会出现这种情况。下面的方法看起来在小数据集上效果很好。然而,我会假设在大型数据集上,它可能没有效率,并且可能有更好的方法来实现这一点

我采取的方法是基于您的问题,即所有数据必须存储在药物名称之间。如果您只想在药物匹配的地方存储产品线,您可以执行以下操作:

result = [[row.strip()] for row in data for med in meds if med in row]

#[['Quetiapine fumarate Drug substance  This document'], ['Povidone    Binder  USP'], ['Lactose monohydrate Diluent USNF'], ['Magnesium stearate  Lubricant   USNF']]
我将药物名称加载到一个列表中,您可能需要根据您的
csv
进行调整

meds = ['Quetiapine fumarate', 'Povidone', 'Magnesium stearate', 'Lactose monohydrate']

with open('1.txt', 'r') as file:
    data = file.readlines()

result = [] # Empty list to store our findings

for idx, row in enumerate(data): # Get index and value of each line in the text file
    count = 0 # Set a counter for each row, this is to determine if there are no matches

    for med in meds:
        if med in row and med not in data[idx-1]: # If medication is matched and the same medication is not found in the previous row
            result.append([row.strip()])
        else: # No match found on this medication, increase counter
            count += 1

    if count == len(meds): # If count == total medication, declare no match and append to previous row
        result[-1].append(row.strip())



for i in result:
    print(i)

#['Quetiapine fumarate Drug substance  This document']
#['Povidone    Binder  USP', 'Povidone new line', "This line doesn't contain any medicine name.", 'Dibasic calcium phosphate dihydrate Diluent USP is not present in the csv']
#['Lactose monohydrate Diluent USNF']
#['Magnesium stearate  Lubricant   USNF']

我在测试文件中添加了
Povidone新行
,以证明如果在同一行中找到相同的药物名称,则会将其追加到最后一个结果。

您是要收集两点之间的所有文本,还是只收集具有药物名称的行?您的示例输出仅显示包含名称,但您的问题似乎针对两个药物名称之间的所有文本。可能重复。谢谢您的答复。在您的输出中,我看到这些行
这一行不包含任何药物名称,csv中不存在二元磷酸钙二水合物稀释剂USP,因为这些行中没有匹配的药物名称,所以输出中不应该存在二元磷酸钙二水合物稀释剂USP。因此,输出应该与我提到的示例输出完全相同,没有文件中的这些额外行。你现在能帮忙吗?是的,代码的顶部部分就是这样做的<代码>结果=[[row.strip()]用于med中med的数据行,如果med在行中]