【问题标题】:Iterating through lists with different lengths遍历不同长度的列表
【发布时间】:2021-03-01 20:32:44
【问题描述】:

我正在尝试遍历 CSV 中的 4 列,每列都包含不同的金额销售 ID。 我制作了一个熊猫数据框并将每一行转换为一个列表。 如果一列的销售 ID 数量多于以下列,则会给我一个错误:

消息:没有这样的元素:无法找到元素:{"method":"xpath","selector":"/html/body/form[1]/div/select/option[@value=nan]" }

但是,如果所有列都具有相同数量的 id,则代码可以正常工作。

    def get_report_data(self):
        current_date = helpers.currentDate
        data = pd.read_csv(r'C:\Users\rford\Desktop\sale_ids.csv')
        everyone_ids = data['Everyone'].tolist()
        dd_ids = data['Daily Deal'].tolist()
        targeted_ids = data['Targeted'].tolist()
        push_ids = data['Push Notification'].tolist()
        acq_ids = data['Acquisition'].tolist()
        for form_code, sales_type, idlist in (
        ( 1, "Everyone", everyone_ids ),
        ( 1, "Daily Deal", dd_ids ),
        ( 2, "Targeted", targeted_ids ),
        ( 2, "Push Notification", push_ids ),
        ( 2, "Acquisition", acq_ids ) ):
            print('Gathering {} Sale Information'.format(sales_type))
            for sale_id in idlist:
                results = []
                helpers.WebDriverWait(helpers.driver, 10)
                helpers.driver.find_element_by_xpath('/html/body/form[{}]/div/select/option[@value={}]'.format(form_code, sale_id)).click()

【问题讨论】:

    标签: python pandas selenium oop


    【解决方案1】:

    内置函数any 可能与每个列表的pop 方法结合使用:

    def get_report_data(self):
        current_date = helpers.currentDate
        data = pd.read_csv(r'C:\Users\rford\Desktop\sale_ids.csv')
        everyone_ids = data['Everyone'].tolist()
        dd_ids = data['Daily Deal'].tolist()
        targeted_ids = data['Targeted'].tolist()
        push_ids = data['Push Notification'].tolist()
        acq_ids = data['Acquisition'].tolist()
        for form_code, sales_type, idlist in (
        ( 1, "Everyone", everyone_ids ),
        ( 1, "Daily Deal", dd_ids ),
        ( 2, "Targeted", targeted_ids ),
        ( 2, "Push Notification", push_ids ),
        ( 2, "Acquisition", acq_ids ) ):
            print('Gathering {} Sale Information'.format(sales_type))
            while any(idlist):
                results = []
                helpers.WebDriverWait(helpers.driver, 10)
                helpers.driver.find_element_by_xpath(
                    '/html/body/form[{}]/div/select/option[@value={}]'.format(
                        form_code, idlist.pop(0)
                    )
                ).click()
    
    

    【讨论】:

      【解决方案2】:

      原来 pandas 正在将 csv 的某些单元格读取为浮点数。 修复最终是在我的数据帧上使用 .fillna(0) ,然后将每一列转换为一个列表,并使用 .astype(int) 将它们设为整数

      df = pd.read_csv(r'C:\Users\rford\Desktop\sale_ids.csv')
              data = df.fillna(0)
              everyone_ids = data['Everyone'].astype(int).tolist()
              dd_ids = data['Daily Deal'].astype(int).tolist()
              targeted_ids = data['Targeted'].astype(int).tolist()
              push_ids = data['Push Notification'].astype(int).tolist()
              acq_ids = data['Acquisition'].astype(int).tolist()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-06-23
        • 2021-04-23
        • 2016-07-23
        • 1970-01-01
        • 1970-01-01
        • 2016-02-23
        • 2015-02-21
        相关资源
        最近更新 更多