簡體   English   中英

如何用python替換列表中的字符串?

[英]How can I replace string in a list with python?

我想將所有這些:'/'更改為:'/',然后在/之前尋找值,並插入一個空格字符。 (所以四得到這個:'8/3',我想要這個:'8/3')這是我的代碼:

 datatable=[] stop = 0 for ctable in soup.find_all('table', "ctable" ): for record in ctable.find_all('tr'): temp_data = [] for data in record.find_all('td'): temp_data.append(data.text.encode('latin-1')) if '/' in data.text: record2 = str(record).replace('/', ' / ') final_format = ' {} '.format(record2) if 'modul' in data.text: stop = 1 break datatable.append(temp_data) if stop == 1: break if stop == 1: break output.writerows(datatable) 

我如何到達?

find_all從功能bs4沒有返回的字符串,但BS標簽,你需要記錄轉換為字符串,以使用替換法。

useful_string = str(record).replace('/', ' / ')

是要走的路。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM