python - 从列表中删除字符

标签 python python-3.x

几个小时以来,我一直在努力解决这个问题,但还没有找到解决方案(也在 SE 上)。 我的问题如下: 我通过 Beautifulsoup 解析 HTML 表格创建了一个列表,它给出了以下内容:

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 字符、空格和最后出现的\xa0\n 。我尝试通过 results = list(map(str.strip, results)) 映射列表,但没有任何变化,列表保持原样。 我是 Python 的新手,即使在查看了此处的其他示例之后,我也没有找到任何对我有用的东西。 提前致谢!

最佳答案

试试这个:

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

关于python - 从列表中删除字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35636560/

相关文章:

python - 如何在Python上将一个数据框添加到另一个数据框

python - 如何在特定范围内规范化 csv 文件中的数据

python - 是否可以在未传递任何内容时将 help 作为默认参数吐出?

python - 如何确定 matplotlib 条形图中条形的顺序

python - Pygame 无法打开声音文件

python - 从头开始将字符串转换为大写

python - 小数点后第二位的停止平均数

windows - 安装 Python 3 后安装 Python 2.7

python - 如何在 Python 3 中迭代模块列表并调用它们的方法

python - 使用 exec() 动态调用异步函数