-1

I'm trying to delete whole specific lines from a text file that contains those number

1801 - 1802 - 1803 until 2000 

sample of the file like

2 1801 0.417
2 1802 0.795
2 1803 0.129
2 1804 0.852

the code I tried in one number as

with open("example.txt", "r") as f:
    lines = f.readlines()
with open("yo.txt", "w") as f:
    for line in lines:
        if line.strip("\n") != "1801":
            f.write(line)

but didn't work and doesn't affect on the file

1 Answers1

1

You are checking if the line is 1801. You should check if 1801 is in the line.

You should use if '1801' in line:

Since you need to check for any number between 1801 and 2000, you need to use a range. The numbers are going to be integers in the range. So convert them to string before you check.

EDIT Ver 2: Read & Write to file:

with open("abc.txt", "r") as f_in, open("out.txt", "w") as f_out:
    for line in f_in:
        if not any(str(i) in line.strip() for i in range(1801, 2001)):
            f_out.write(line)

Input file:

2 1801 0.417 # do not write to output file
2 1802 0.795 # do not write to output file
2 1750 0.234 # write to output file
2 1803 0.129 # do not write to output file
2 1779 0.123 # write to output file
2 1804 0.852 # do not write to output file
2 1760 0.345 # write to output file

Output File:

2 1750 0.234 # write to output file
2 1779 0.123 # write to output file
2 1760 0.345 # write to output file

EDIT Ver 1: Concept logic

Try this. Instead of a file, I am just putting the whole data into a string and processing. Let me know if you want a full file read and write implementation.

sample_data = '''2 1801 0.417
2 1802 0.795
2 1750 0.234
2 1803 0.129
2 1779 0.123
2 1804 0.852
2 1750 0.345'''

for line in sample_data.split('\n'):
    if not any(str(i) in line for i in range(1801, 2001)):
        print ('written to file - line :',line)
    else:
        print ('skipped line ',line)

The output of this will be:

skipped line  2 1801 0.417
skipped line  2 1802 0.795
written to file - line : 2 1750 0.234
skipped line  2 1803 0.129
written to file - line : 2 1779 0.123
skipped line  2 1804 0.852
written to file - line : 2 1750 0.345
Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33
  • thanks for replying but i need to delete them not printing another text –  Feb 02 '21 at 03:52
  • 1
    The solution was the logic to be implemented. Let me write the full code that can read the file and write only those lines that do not have numbers between 1801 & 2000 – Joe Ferndz Feb 02 '21 at 03:54
  • @user5520049 Does the new answer help? – Joe Ferndz Feb 02 '21 at 04:03
  • 1
    Very elegant and nice. Thumb up. Just upvote it... – Daniel Hao Feb 02 '21 at 04:12
  • @DanielHao, thanks. Use upvote for thumbs up. Thx – Joe Ferndz Feb 02 '21 at 04:14
  • 1
    @DanielHao, I spent a lot of time on these websites and free courses on Coursera. [w3 python](https://www.w3resource.com/python/python-tutorial.php), [w3 pandas](https://www.w3resource.com/pandas/index.php), https://www.learnpython.org/, https://realpython.com/start-here/, a few youtube videos, coursera, and daily resolving Stack Overflow questions and reviewing answers from some of the top responses on Stack Overflow – Joe Ferndz Feb 02 '21 at 04:22
  • @DanielHao, see this response as well https://stackoverflow.com/questions/66003951/how-can-i-print-the-lines-like-this-example/66004319#66004319 – Joe Ferndz Feb 02 '21 at 06:19
  • Thanks for the tips... will study from these sources. – Daniel Hao Feb 02 '21 at 12:52
  • @JoeFerndz - Just FYI share this with you - to see if you have better solution. https://stackoverflow.com/a/66006610/10760768 (for fun...) – Daniel Hao Feb 02 '21 at 13:53