从文本文件中删除特殊字符



我的input.txt如下

nn            n        nn    nnrnrn    rnrnrnrnrnrnrnrnrnrn        rn        rn    rnrnrn    rnrnrnrn        rnrnrnrnrnrnrn        hello boysrn        boysrn    boysrnrn         Download PDFrn        PDFrn    Xrn    Close Windowrnrnrnrn    
This boys text has undergone conversion so that it is mobile and web-friendly. This may have created formatting or alignment issues. Please refer to the PDF copy for a print-friendly version.
rnrnrnrnrnrnrn    rn        
BOYS CLUB AUSTRALIA
rn
26 July 2019
rn
hello boys
rn
rnhello boys
rn
--------------------------------------------------------------------------------------------------------------------------------------
rn
Introduction
rnrn    
1. rn    
This letter to let you know that your application has been successful with our school
rn  

我想删除不必要的模式就像" n n" r r"," r n r n r n r n r n r n r n r n r n r n"在解析时,我想删除所有的特殊模式,只希望有文本和数字。

我已经试过了。

with open (data, "r", encoding='utf-8') as myfile:
for line in myfile:
line.rstrip()
line = re.sub(r'rn', '', line)
with open("out.txt", "a",  encoding='utf-8') as output:
output.write(line)

但是即使'rn'也没有在输出文件中被删除。谢谢。

您可以使用replace()方法将rn子字符串替换为空字符串。

with open(data, 'r', encoding='utf-8') as myfile:
with open('out.txt', 'a',  encoding='utf-8') as output:
for line in myfile:
output.write(line.replace('rn', '').rstrip())

要在Python中从文本文件中删除特殊字符,例如换行字符(n),可以使用str类的replace()方法。此方法允许您将特定的字符或字符串替换为另一个字符或字符串。

您可以使用replace,但也可以使用str.isprintable来过滤掉不可打印的字符:

input_file = 'input.txt'
output_file = 'output.txt'
with open (input_file, "r", encoding='utf-8') as infile:
with open(output_file, "w", encoding='utf-8') as outfile:
for line in infile:
outfile.write(
''.join(filter(str.isprintable, line.replace('\n', '').replace('\r', '')))
+ 'n'
)

所以,输出是:

  hello boys        boys    boys         Download PDF        PDF    X    Close Window    
This boys text has undergone conversion so that it is mobile and web-friendly. This may have created formatting or alignment issues. Please refer to the PDF copy for a print-friendly version.

BOYS CLUB AUSTRALIA
26 July 2019
hello boys
hello boys
--------------------------------------------------------------------------------------------------------------------------------------
Introduction

1.     
This letter to let you know that your application has been successful with our school