Why isn't the replace() function working?

别来无恙 提交于 2021-02-17 01:57:07

问题


I'm scraping a website using Selenium. When I get the text of a list of elements (headers), this is what it prints:

    ['Countyarrow_upward Reportingarrow_upward Totalarrow_upward Bennet (D)arrow_upward Biden (D)arrow_upward Bloomberg (D)arrow_upward Booker (D)arrow_upward Boyd (D)arrow_upward Buttigieg (D)arrow_upward 
Castro (D)arrow_upward De La Fuente III (D)arrow_upward Delaney (D)arrow_upward Ellinger (D)arrow_upward Gabbard (D)arrow_upward Greenstein (D)arrow_upward Klobuchar (D)arrow_upward Patrick (D)arrow_upw
ard Sanders (D)arrow_upward Sestak (D)arrow_upward Steyer (D)arrow_upward Warren (D)arrow_upward Williamson (D)arrow_upward Yang (D)arrow_upward']

I obviously only want the names and the "(D)", so I tried using the replace() function to replace the Countyarrow_upward Reportingarrow_upward Totalarrow_upward and arrow_upward with an empty string. Here's my code:

headers = driver.find_elements_by_xpath('//*[@id="content"]/div/div[3]/div/div[2]/div/div[2]/div/div[2]/div[1]/div/table/thead/tr[1]')
    header_text = []
    for i in headers:
        header_raw_text = i.text
        header_raw_text.replace("Countyarrow_upward Reportingarrow_upward Totalarrow_upward ", "")
        header_raw_text.replace("arrow_upward ", "")
        header_text.append(header_raw_text)

    print(header_text)

When I run this code, I get the same thing above, and the replace() function doesn't work.

Help is much appreciated!


回答1:


strings are immutable. so header_raw_text.replace() does not change the string itself.you have to do reassign the result after replacing.

header_raw_text = header_raw_text.replace("arrow_upward ", "")


来源:https://stackoverflow.com/questions/61178595/why-isnt-the-replace-function-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!