0

I have a txt file with elements:

705.95 117.81 1242.00 252.43 5.02

1036.12 183.52 1242.00 375.00 1.96

124.11 143.43 296.91 230.32 10.70

0.00 0.00 0.00 0.00 4.84

0.00 6.60 112.99 375.00 17.50

0.00 186.66 14.82 375.00 8.23

695.36 162.75 820.66 263.08 12.84

167.61 134.45 417.75 222.10 27.61

0.00 0.00 0.00 0.00 6.86

0.00 0.00 0.00 0.00 11.76

I want to delete lines that contains 0.00 0.00 0.00 0.00 as the first four elements of each line, how can I do that using python? Your help is highly appreciated.

3
  • 2
    What have you written? Please try first before asking... Commented Oct 30, 2021 at 2:38
  • 2
    what have you tried, and where did you get stuck. Good questions typically include a bit of your code, and either an expected output vs what you're getting now, or whatever error message you're currently up against. Please edit your question to include these details. Commented Oct 30, 2021 at 2:38
  • Good luck with your project, you will need some code for it. Commented Oct 30, 2021 at 2:42

2 Answers 2

4
with open('file.txt', 'r') as infile:
  with open('output.txt', 'w') as outfile:
    for line in infile:
      if not line.startswith('0.00 0.00 0.00 0.00'):
        outfile.write(line)

Here we open file.txt with your lines for reading and output.txt for writing the result. Then, we iterate over each line of the input file and write the line in the results file if it doesn't start with '0.00 0.00 0.00 0.00'.

Sign up to request clarification or add additional context in comments.

Comments

1

If you want to overwrite the files without creating new output files, you can try this. The following code also helps you iterate through all the text files in the current directory.

import glob

for i in glob.glob("*.txt"):
    with open(i, "r+") as f:
        content = f.readlines()

        f.truncate(0)
        f.seek(0)

        for line in content:
            if not line.startswith("0.00 0.00 0.00 0.00"):
                f.write(line)

4 Comments

@SoodepDhakal Sure, I'm ready to help. I am pretty happy today! Just tell me which format you want and add some more details.
I used the same approach for the file in format: [tensor([[1.7744e+02, 4.7730e+02, 1.2396e+02, 1.1678e+02, 5.9988e-01], new line [7.8410e+02, 1.7532e+02, 6.2769e+02, 2.1083e+02, 9.9969e-01], device='cuda:0')] I want my output to look like this: 177.44 4.77.30 1.23.96 1.16.78 5.9.988 new line 784.10 175.32 627.69 210.83 99.969 I cant format the text here so I just wrote new line (that means next line is in another line)
@SoodepDhakal I think you should ask a new question because someone may provide a better approach than mine. But I would use Regular Expression (import the re module) to parse the text. However, I have no idea how to convert scientific notation into float. So I think you should ask a new question with a title like "How can I parse a text file with scientific notation and turn them into float". That is just a hint.
@SoodepDhakal Ok, I see you are struggling with the "what have you tried" part. Just say that you have tried using Regex to parse the numbers but scientific notations are too complex to parse and you need some help. Just don't be afraid to ask a question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.