Python变量在if语句中不起作用

时间:2018-10-11 04:11:02

标签: python pycharm xlrd python-3.7

我正在使用硒从txt文件中遍历URL,并获取每个URL上列出的特定序列号。然后,我想引用一个Excel文件并找到序列号旁边的单元格并打印该值。

我的问题是,当我将序列号存储在变量中时,它在if语句中不起作用。如果我将变量替换为实际的序列号,它将按预期运行。为什么存储的变量在我的代码中不起作用?

这是我想要的代码,我没有收到任何输出:

f1 <- function(x)  {
                x1 <- as.Date(paste0(x, "-01") )
                x2 <- as.integer(difftime(x1[-1], x1[-length(x1)], unit = "weeks"))
                  max(rle(c(x2[1], x2))$lengths) 
                  }

dat %>% 
   group_by(id, card_id) %>%
    summarise(max_count = f1(bill_date))        
# A tibble: 4 x 3
# Groups:   id [?]
#  id    card_id max_count
#  <chr> <chr>       <int>
#1 Adam  0001            2
#2 Adam  0002            3
#3 Bob   0003            3
#4 Bob   0004            5

如果我将序列号替换为数字,则会得到要查找的单元格值:

 with open('Url.txt') as links:
        for line in links:
            time.sleep(3)
            driver.get(line)
            editpage = driver.find_elements_by_link_text('Edit this device')
            for page in editpage:
                time.sleep(1)
                editurl = page.get_attribute('href')
                driver.get(editurl)
                Devicesn = driver.find_elements_by_id('device.name')
                for sn in Devicesn:
                    serialnumber = sn.get_property('value')
                    wb = xlrd.open_workbook('C:\doc.xls')
                    for sheet in wb.sheets():
                        for row_num in range(sheet.nrows):
                            row_value = sheet.row_values(row_num)
                            if row_value[1] == serialnumber:
                                print(row_value[0])

这给了我我想要的价值。为什么我的序列号变量在这里不起作用?谢谢!

编辑:谢谢大家的答复。为了希望能更有意义,我打印了序列号变量及其长度。

    with open('Url.txt') as links:
        for line in links:
            time.sleep(3)
            driver.get(line)
            editpage = driver.find_elements_by_link_text('Edit this device')
            for page in editpage:
                time.sleep(1)
                editurl = page.get_attribute('href')
                driver.get(editurl)
                Devicesn = driver.find_elements_by_id('device.name')
                for sn in Devicesn:
                    serialnumber = sn.get_property('value')
                    wb = xlrd.open_workbook('C:\doc.xls')
                    for sheet in wb.sheets():
                        for row_num in range(sheet.nrows):
                            row_value = sheet.row_values(row_num)
                            if row_value[1] == 6216295237: #replaced serialnumber
                                print(row_value[0])

输出是:6216295237            10

VS

serialnumber = sn.get_property('value')
wb = xlrd.open_workbook('C:\doc.xls')
for sheet in wb.sheets():
    for row_num in range(sheet.nrows):
        row_value = sheet.row_values(row_num)
        print(serialnumber)
        print(len(serialnumber))
        time.sleep(60)
        if row_value[1] == serialnumber:
            print(row_value[0])

输出为:G47

3 个答案:

答案 0 :(得分:1)

这行中您很可能没有获得正确的值或根本没有值

serialnumber = sn.get_property('value')

您可以尝试打印此变量以查看得到的内容。 也许您要查找的文本实际上不在ID为'value'的元素的'device.name'属性中。

此外,id通常是唯一的,因此整个页面中应该只有一个。也许'device.name'实际上是一堂课?没有页面来源就无法分辨。

答案 1 :(得分:1)

尝试打印变量:

print(serialnumber)

print(row_value[1])

然后,检查它们是否相同。

编辑:查看文档,您应该尝试使用get_attribute()代替get_property

答案 2 :(得分:0)

DYZ用户在评论中回答了这个问题。

sn.get_property返回的值是一个字符串。您必须先将其转换为数字– DYZ

我只是用它来转换它:

Auth.auth().createUser(withEmail: userEmail,password: userPassword, completion: {(User, error) in
    if error != nil {
        print(error as Any)
        return
    }
    guard let uid = User?.user.uid else {return}
    let ref = Database.database().reference(fromURL:"Database-URL")
    let userReference = ref.child("users").child(uid)

    let values = ["Firstname": userFirstName,"email": userEmail]
    userReference.updateChildValues(values, withCompletionBlock: { (error, reference) in
        if error != nil {
            print(error as Any)
            return
        }
    })
})