删除python中文本文件的特定模式

2024-09-27 20:20:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个名为file1的输入文件,其中包含:

Student 0 : Performed well but can do better. [76.50%]

Student 1 : Brilliant performance. [98.50%]

在这个特定的文件中,我只想删除%部分,以便它生成如下输出:

Student 0 : Performed well but can do better.

Student 1 : Brilliant performance.

我试着这样做:

with open('file1', 'r') as infile, open('file2', 'w') as outfile:
    temp = infile.read().replace("[[0-9]+]", "").replace("%","")
    outfile.write(temp)

但这只是删除%符号并将输出作为:

Student 0 : Performed well but can do better. [76.50]

Student 1 : Brilliant performance. [98.50]


Tags: 文件asperformanceopendocanfile1student

热门问题