如何从列表结果中删除/n?

how to remove /n from list results?

大家好我正在抓取 table 并将 headers 和 table 的 body 分成单独的列表,但 body 数据有很多 '/n',我正在尝试删除它们,但我似乎无法将它们删除。

代码:

soup = BeautifulSoup(driver.page_source,'html.parser')
table= soup.find("table")
rows= table.find_all("tr")
table_contents = []
for tr in rows:
    if rows.index(tr)== 0:
        row_cells = [ th.getText().strip() for th in tr.find_all('th') if th.getText().strip() !='']
    else:
        row_cells = ([ tr.find('th').getText() ] if tr.find('th') else [] ) + [ td.getText().strip() for td in tr.find_all('td') if td.getText().strip() != '' ] 
    if len(row_cells) > 1 : 
        table_contents += [ row_cells ]
table_head= table_contents[0]
table_body= table_contents[1]
print (table_head)
print (table_body)

结果:

table head= ['Student Number', 'Student Name', 'Placement Date']
table body= ['20808456', 'Sandy\n(f) \nGurlow', '01/13/2023']

正如您在 table body 结果中看到的那样,'\n' 妨碍了我,我知道如何摆脱它。因为我有 100 个样本要解决同样的问题。

使用 str.replace() 和列表理解:

[i.replace('\n', '') for i in table_body]

输出:

['20808456', 'Sandy(f) Gurlow', '01/13/2023']