2016-02-25 56 views
3

我試圖讓我的頭幾個小時,但尚未找到解決方案(也在這裏SE)。 我的問題如下:從列表中刪除字符

results = [[None, ' Windows 64 bit\n\t\t'], ['\n\t Official\n\t'], ['blender-2.76-3f602ff-win64.zip'], ['108M'], ['\n\t Thu Feb 25 09:21:53 2016\n\t'], [None, ' Mac OS X 64 bit\n\t \t\t'], ['\n\t Official\n\t'], ['blender-2.76-ba98b68-OSX-10.6-x86_64.zip'], ['113M'], ['\n\t Thu Feb 25 09:57:40 2016\n\t'], [None, ' Windows 32 bit\n \t'], ['\n\t Official\n\t'], ['blender-2.76-ba98b68-win32.zip'], ['90M'], ['\n\t Thu Feb 25 11:33:10 2016\n\t'], [None, ' Linux 32 bit\n\t \t\t'], ['\n\t Official\n\t'], ['blender-2.76-3f602ff-linux-glibc211-i686.tar.bz2'], ['106M'], ['\n\t Thu Feb 25 08:33:43 2016\n\t'], [None, ' Linux 64 bit\n\t \t\t'], ['\n\t Official\n\t'], ['blender-2.76-3f602ff-linux-glibc211-x86_64.tar.bz2'], ['108M'], ['\n\t Thu Feb 25 08:33:24 2016\n\t'], ['\xa0\n'], ['\xa0\n'], ['\xa0\n'], ['\xa0\n'], ['\xa0\n']] 

現在我想刪除\ n和\ t字,空格和: 我通過解析通過Beautifulsoup一個HTML表,這給了我下面創建的列表\ xa0 \ n發生在最後。我試圖通過results = list(map(str.strip, results))映射列表,但沒有任何變化,列表保持原樣。 我是新來的Python,即使在這裏查看其他例子後,我沒有找到任何對我有用的東西。 在此先感謝!

回答

4

試試這個:

results = [[item.strip().strip("\xa0") if item is not None else None for item in sublist] for sublist in results] 
+0

「我想刪除\ n和\ t字,空白......」所以這會工作,但你的'strip'是OP的情況下,過於狹窄。 –

+0

你是什麼意思?我用'.strip()'移除所有空格,然後用'.strip(「\ xa0」)''移除'「\ xa0」'。缺乏什麼? – zondo

+0

對不起,我讀得太快了,我的思緒跳過了重複的脫衣舞。繼續:) –