1

I have an input file named file1 which contains:

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

Student 1 : Brilliant performance. [98.50%]

In this particular file I just want to remove the % part so that it produces output like:

Student 0 : Performed well but can do better.

Student 1 : Brilliant performance.

I tried in this manner:

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

But this is only removing the %sign and giving output as:

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

Student 1 : Brilliant performance. [98.50]

0

1 Answer 1

1

You still need regex:

import re
with open('file1', 'r') as infile, open('file2', 'w') as outfile:
    temp = re.sub("\[[\d+\.%]+\]", "", infile.read())
    outfile.write(temp)
Sign up to request clarification or add additional context in comments.

4 Comments

It is giving a warning saying : "FutureWarning: Possible nested set at position 1 temp = re.sub("[[0-9]+]", "", infile.read()).replace("%","")"
@Dev Check my edit
Yes, thanks. Now I am getting my desired output but that future warning is still there. Can you help?
@Dev Check my edit

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.