agreüs
agreüs

Reputation: 109

Delete lines which contains numbers in python

I have to copy a file but before that I need to remove the lines with numbers.

this is my file:

0:00:00.000,0:00:06.410
let's start with

0:00:04.470,0:00:10.769
something

0:00:06.410,0:00:12.360
easy

and I want to have something like this:

let's start with something easy

and this is my code :

readFile = open("file.sbv")
writeFile = open("newwordlist.sbv","w")

for line in readFile: 
    newline = line.rstrip('\r\n')
    writeFile.write(newline)
readFile.close()
writeFile.close()

Upvotes: 2

Views: 1293

Answers (2)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521437

You could try using re.search to identify the number lines. Assuming the line does not match this pattern, then you would write it out:

readFile = open("file.sbv")
writeFile = open("newwordlist.sbv","w")

for line in readFile: 
    newline = line.rstrip('\r\n')
    if not re.search(r'^\d:\d{2}:\d{2}\.\d{3},\d:\d{2}:\d{2}\.\d{3}$', newline, re.M|re.I)
        writeFile.write(newline)
readFile.close()
writeFile.close()

This answer assumes that maybe you do have lines which contain numbers, but you don't want to remove those. The pattern I used targets the timestamp lines in your sample data.

Upvotes: 1

user3483203
user3483203

Reputation: 51155

You don't need regex for this, you can simply use any(i.isdigit() for i in line):

with open('in.txt') as infile, open('out.txt', 'w') as outfile:
  for line in infile:
    if not any(i.isdigit() for i in line):
      outfile.write(line)

in.txt

0:00:00.000,0:00:06.410
let's start with
0:00:04.470,0:00:10.769
something
0:00:06.410,0:00:12.360
easy

out.txt

let's start with
something
easy

If you want the results on one line, you can write line.rstrip('\r\n') instead of line, however this is dependent on which line-endings your file is using, it may be just \n.

Upvotes: 2

Related Questions