写一些正则表达式来清理字符串列表

时间:2019-02-20 16:57:12

标签: python string list

我有一个列表,其中包含来自docx文件的文本数据。我如何应用一些正则表达式或一些lambda函数来遍历整个列表并“清理”它;意思是,我想取出\t\n

['\tSA   [WP5]\t\t\n', "<class 'docx.text.paragraph.Paragraph'>\n", '\t\tCOUNTRY:\n', "<class 'docx.text.paragraph.Paragraph'>\n", '\n']

这样我的输出看起来像:

['SA   [WP5]', "<class 'docx.text.paragraph.Paragraph'>", 'COUNTRY:', "<class 'docx.text.paragraph.Paragraph'>", '']

1 个答案:

答案 0 :(得分:1)

[x.replace('\t', '').replace('\n', '') for  x in lst] 

可以做到这一点,而无需正则表达式。