我有一些测试数据刮和斗争,如何清理它正确的效率方面。你知道吗
test_data = ['\r\n \r\n ',
'\r\n ',
'Reine Baumwolle',
'\r\n ',
'\r\n \r\n ',
'\r\n ',
'Kontrastblende am Ausschnitt',
'\r\n ',
'\r\n \r\n ',
'\r\n ',
'Pflegeleicht',
'\r\n ',
'\r\n \r\n ']
通过正则表达式到目前为止,我可以清理数据:
import re
rep = ['\n','\t','\r']
for y in rep:
test_data = [r.replace(y, '') for r in test_data]
我的中期结果如下:
[' ',
' ',
'Reine Baumwolle',
' ',
' ',
' ',
'Kontrastblende am Ausschnitt',
' ',
' ',
' ',
'Pflegeleicht',
' ',
' ']
如何扩展函数以清除所有不必要的空格和逗号,从而只得到一个字符串,如下所示:
预期结果:
test_data = 'Reine Baumwolle, Kontrastblende am Ausschnitt, Pflegeleicht'
或仅使用逗号分隔:
test_data = 'Reine Baumwolle Kontrastblende am Ausschnitt Pflegeleicht'
使用
list comprehension.
例如:
输出:
str.strip
删除任何前导或尾随空格。你知道吗看起来您只需要过滤掉那些从空白中去掉后就什么都没有了的字符串。你知道吗
相关问题 更多 >
编程相关推荐