【问题标题】:Ignoring character when converting转换时忽略字符
【发布时间】:2019-07-13 20:27:40
【问题描述】:

使用命令:

cena_postov=driver.find_elements_by_xpath("//*[starts-with(@id, 'td_')]/td[11]/a[1]/span/span")
for spisok_cen in cena_postov:
  print(spisok_cen.get_attribute('textContent'))

我得到一个文本列表。我想将其转换为数字使用

if int(spisok_cen.get_attribute('textContent'))<=10 and int(spisok_cen.get_attribute('textContent'))>=12000:
 print('anything')

但是'这个符号让我很困扰。 我收到一个错误invalid literal for int() with base 10: ": 6'000 如何让python忽略这个字符?

【问题讨论】:

    标签: python python-2.7 selenium selenium-webdriver selenium-chromedriver


    【解决方案1】:

    您可以使用replace 方法删除不必要的':

    if int(spisok_cen.get_attribute('textContent').replace("'", ""))<=10 and int(spisok_cen.get_attribute('textContent').replace("'", ""))>=12000:
        print('anything')
    

    您也可以通过locale here 找到解决方案。

    【讨论】:

    • 我又添加了几个 .replace("'", "").replace(":", "").replace(" ", "") 并且成功了。谢谢,我稍后再看第二种方法。
    猜你喜欢
    • 2018-10-28
    • 2019-10-21
    • 1970-01-01
    • 2021-07-15
    • 2019-05-26
    • 2020-01-30
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多