简体   繁体   English

我想使用过滤条件从 excel 中检索数据,如果过滤条件匹配,那么数据应该在 python 中迭代吗?

[英]I want to retrieve the data from excel using filter criteria if filter matches then data should be iterated in python?

I want to retrieve the data from excel using filter criteria if filter matches then data should be iterated in python.我想使用过滤条件从 excel 中检索数据,如果过滤条件匹配,那么数据应该在 python 中迭代。 Below is the code working without filter if add filter it is not working can any one please help me out.以下是没有过滤器的代码,如果添加过滤器不起作用,任何人都可以帮助我。

Can any one provide me solution using other framework in python i need to get row values using filter value.任何人都可以使用 python 中的其他框架为我提供解决方案吗?我需要使用过滤器值获取行值。

Here am using openpyxl这里我使用 openpyxl

from openpyxl import Workbook, load_workbook

class Util:

    def read_data_from_excel(file_name, sheet, filter):
        datalist = []
        wb = load_workbook(filename=file_name)
        sh = wb[sheet]
        row_ct = sh.max_row
        col_ct = sh.max_column

        for i in range(2, row_ct + 1):
            row = []
            for j in range(1, col_ct + 1):
                if (sh.cell(row=i, column=j).value == filter): # if i remove this line it is working
                    row.append(sh.cell(row=i, column=j).value)
            datalist.append(row)
        return datalist
#calling like this
from base_testcase import BaseTestCase
from ddt import ddt, data, file_data, unpack
from util.util import Util

@ddt
class GithubTest(BaseTestCase):

    @data(*Util.read_data_from_excel("./test_data.xlsx", "LoginData", "github")) # github is my filter
    @unpack
    def test_notifications(self, username, password):
        self.login(username, password)
        self.check_notification()
class LoginPage(object):
    url = 'http://github.com'
    sign_in_link = "a:contains('Sign in')"
    username = "[name='login']"
    password = "[name='password']"
    sign_in = "[value='Sign in']"
from seleniumbase import BaseCase
from login_page import LoginPage

class BaseTestCase(BaseCase):
   def login(self, username, password):
        self.open(LoginPage.url)
        self.click(LoginPage.sign_in_link)
        self.type(LoginPage.username, username)
        self.type(LoginPage.password, password)
        self.click(LoginPage.sign_in)
# My Excel data
username | password | filter_name | #--> from this column filter should be taken
sample   | tester   | github      |
test     | user     | tester      |

I changed the filter to take the entire row if one of the columns in that row has a value that equals to filter i tried it on an XML file of my own and it works hope i could help:)如果该行中的一列具有等于filter的值,我将过滤器更改为获取整行

this should do the trick:这应该可以解决问题:

from openpyxl import Workbook, load_workbook

def read_data_from_excel(file_name, sheet, filter):
    datalist = []
    wb = load_workbook(filename=file_name)
    sh = wb[sheet]
    row_ct = sh.max_row
    col_ct = sh.max_column

    for i in range(2, row_ct + 1):
        row = []
        for j in range(1, col_ct + 1):
            if (sh.cell(row=i, column=j).value == filter): # if i remove this line it is working
                datalist.append([col.value for col in  sh[i]])
                break
        datalist.append(row)
    return datalist

if you have any questions about the code feel free to ask me in the comment and if my comment helped you please consider upvoting or marking it as the answer:)如果您对代码有任何疑问,请随时在评论中问我,如果我的评论对您有帮助,请考虑投票或将其标记为答案:)

I have achieve this using pandas in python its very simple我已经在 python 中使用pandas实现了这一点,它非常简单

def read_excel_data(file_path, sheet, filter):
        df = pd.read_excel(file_path, sheet)
        return df[df['filter'] == filter].to_dict('records')

# Here once you got the records then you can iterate and get value you want
login_data = read_excel_data('path to file', sheet_name, filter_varable)

for data in login_data:
    print(data['username'])
 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM