函数返回csv中具有输入字符串的列的第一个精确匹配

2024-05-23 14:28:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用python编写一个函数,返回csv中一列和一个输入字符串的第一个精确匹配。你知道吗

这是输入csv。药品列表是标题。我想检查输入文件(string)中的6个字符串,代码应该返回输入字符串中药品列表中第一个完全匹配的名称。你知道吗

要检查的关键字:

Drug product(s) list

solution for injection
suspension for injection
film-coated tablet
concentrate for solution for infusion
cream
modified-release tablet

应检查关键字的输入字符串:

 This supplemental application, submitted as a “Changes Being Effected” supplement, proposes  
the revisions in the incubation scheme for routine environmental monitoring of swab samples of film-coated tablet Alpuzim 
with the incubation scheme for contact plates. Also submission for Alpuzim cream is pending.

该函数只应返回文件中的第一个匹配项,即薄膜涂布片。一旦找到第一个匹配项,输入字符串中的循环就应该中断

这就是我一直尝试的:

import csv
def first_Occurrence(string):
    with open('C:\\Users\\Lenovo\\.spyder-py3\\unique_final_drug_products.csv', newline='', encoding ='utf-8') as myFile:

        reader = csv.reader(myFile)
        for row in reader:
            product = row[0].lower()

    for i in range(len(string)):
        i = i.lower()
        if(string[i] == product):
            return i
    return ('string not found')


a = first_Occurrence('we have a solution cream for back pain')
print(a)

Tags: 文件csvthe函数字符串in列表for
2条回答

我不是要解决你的问题,而是the logic to what you are looking for is here

import csv

login = False
answer = input("Do you have an account?(yes or no) ")

if answer == 'yes' :
   with open('upassword.csv', 'r') as csvfile:
      csv_reader = csv.reader(csvfile)
      username = input("Player One Username: ")
      password = input("Player One Password: ")

      for row in csv_reader:
         print(row[0], row[1])
         print(username, password)
         if row[0]== username and row[1] == password:
            login = True
            break
         else:
            login = False
            break

   if login == True:
      print("You are now logged in!")
   else:
      print("Incorrect. Game Over.")
      exit()    
else:
   print('Only Valid Usernames can play. Game Over.')
   exit()

import csv
from pathlib import Path

def first_occurrence(string):
    path = Path('C:/Users/Lenovo/.spyder-py3/unique_final_drug_products.csv')
    with path.open() as f:
        reader = csv.reader(f)
        for row in reader:
            if string.lower() in row[0].lower():
                return row


a = first_occurrence('we have a solution cream for back pain')
print(a)

相关问题 更多 >