当前位置: 代码迷 >> python >> python从文本替换\\ n
  详细解决方案

python从文本替换\\ n

热度:34   发布时间:2023-06-13 16:51:35.0

这是一个文本文件示例:

'15235457345', '', '\n\nR\n\nE\nM\nO\n\nV\nE\nD\n', '1445133666', 'nick', '', '1236500', 'git', '', '', '123face', '2015-10-18 ', '2015-10-23 ', 'name', 'great', 'sha', '\n\nB\n\nU\n\nT\n\nH\nO\nW\n', '1445123147'

我想删除一些碎片

\n\nR\n\nE\nM\nO\n\nV\nE\nD\n

\n\nB\n\nU\n\nT\n\nH\nO\nW\n

我使用remove和buthow来找出问题,但在实际中,这些是\\ timestamp等。

le = ['15235457345', '', '\n\nR\n\nE\nM\nO\n\nV\nE\nD\n', '1445133666', 'nick', '', '1236500', 'git', '', '', '123face', '2015-10-18 ', '2015-10-23 ', 'name', 'great', 'sha', '\n\nB\n\nU\n\nT\n\nH\nO\nW\n', '1445123147']
print [value for value in le if '\n' not in value]

输出:

['15235457345', '', '1445133666', 'nick', '', '1236500', 'git', '', '', '123face', '2015-10-18 ', '2015-10-23 ', 'name', 'great', 'sha', '1445123147']
s='15235457345', '', '\n\nR\n\nE\nM\nO\n\nV\nE\nD\n', '1445133666', 'nick', '', '1236500', 'git', '', '', '123face', '2015-10-18 ', '2015-10-23 ', 'name', 'great', 'sha', '\n\nB\n\nU\n\nT\n\nH\nO\nW\n', '1445123147'

for i in range(0,len(s)):
    print s[i].replace('\n','')

输出:

15235457345

REMOVED
1445133666
nick

1236500
git


123face
2015-10-18 
2015-10-23 
name
great
sha
BUTHOW
1445123147

希望这是您想要的。