如何洗掉 ['公司名稱', '總部位置', '公司型別\n\r\n\t\t\t\t\t\t\t\t?', '中的\n\r\t機隊規模']
for i in head:
c = i.text.strip()
a.append(c)
print(a)```
*Output**
['Company Name',
'Headquarters Location',
'Company Type\n\r\n\t\t\t\t\t\t\t\t?',
'Fleet Size']
uj5u.com熱心網友回復:
請不要燒毀我的變數名
a=['Company Name',
'Headquarters Location',
'Company Type\n\r\n\t\t\t\t\t\t\t\t?',
'Fleet Size']
b = []
unwanted = ["\n","\t","\r"]
for i in a:
to_add = ""
for char in i:
if char not in unwanted:
to_add = char
b.append(to_add)
print(b)
uj5u.com熱心網友回復:
我認為正則運算式模塊會很有效。
import re
for i in head:
c = i.text.strip()
# ==== regex substitution ====
c = re.sub(r'[\r\n\t]', '', c, flags=re.MULTILINE)
a.append(c)
print(a)
*Output**
['Company Name',
'Headquarters Location',
'Company Type?',
'Fleet Size']
uj5u.com熱心網友回復:
使用re.sub:
import re
lst = ['Company Name', 'Headquarters Location', 'Company Type\n\r\n\t\t\t\t\t\t\t\t?', 'Fleet Size']
output = [re.sub(r'[\r\n\t]', '', x) for x in lst]
print(output) # ['Company Name', 'Headquarters Location', 'Company Type?', 'Fleet Size']
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/322327.html
上一篇:如果沒有列名,如何提取值?
