簡體   English   中英

如何刪除列表python中的\\ t \\ n \\ r進行網絡抓取?

[英]how to remove \t\n\r in list python doing web Scraping?

如何刪除['公司名稱', '總部位置', '公司類型\\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'] 

使用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']

請不要燒毀我的變量名

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)

我認為正則表達式模塊會很有效。

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'] 

暫無
暫無

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

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