1. 程式人生 > >去除列表中的 和空字符

去除列表中的 和空字符

列表 div 發現 code bsp class urn \n 中間

s=[\n, magnet:?xt=urn:btih:060C0CE5CFAE29A48102280B88943880689859FC\n]

上面是目標代碼,一個列表,中間有\n,我們現在將其去掉

s=[x.strip() for x in magnet_link]

運行會發現結果為

s=[‘‘, magnet:?xt=urn:btih:060C0CE5CFAE29A48102280B88943880689859FC]

離我們的要求越來越近了

s=[x.strip() for x in magnet_link if x.strip()!=‘‘
]

好了,結果出來了

s=[magnet:?xt=urn:btih:060C0CE5CFAE29A48102280B88943880689859FC]

去除列表中的\n 和空字符