這個問題在這里已經有了答案: 如何過濾字串串列? (4 個回答) 8 小時前關閉。
我有字串串列,我想從字串中過濾掉非英文字符。
list=['??? ??????? ??????, from the ??? ?????? M Ramalinga Reddy,
'23/2 ?????? ?????? 18, 1?? ???? ?????, 23/2 Ward No 18, Cross,']
我的代碼:
regex = re.compile("[^a-zA-Z0-9!@#$&()\\-`. ,/\"] ")
for i in list:
li = regex.sub(' ', i)
print(li)
我的輸出
[, from the M Ramalinga Reddy,
23/2 18, 1 , 23/2 Ward No 18, Cross,]
我的愿望輸出
[M Ramalinga Reddy,
23/2 Ward No 18, Cross]
uj5u.com熱心網友回復:
假設我們將您的問題表述為在串列中的每個字串中找到最終的英語單詞,我們可以re.findall在這里嘗試使用:
# -*- coding: utf-8 -*-
import re
inp = ['??? ??????? ??????, from the ??? ?????? M Ramalinga Reddy, D No',
'23/2 ?????? ?????? 18, 1?? ???? ?????, 23/2 Ward No 18, fst Cross,']
output = [re.findall(r'[a-zA-Z0-9"/] (?: [a-zA-Z0-9!@#$&()\\-`. ,/\"] )*$', x) for x in inp]
print(output)
# ['M Ramalinga Reddy, D No', '23/2 Ward No 18, fst Cross,']
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/410142.html
標籤:
