Vahideh
Vahideh

Reputation: 45

How to delete numbers at the start of lines of a file

I have some files containing a lot of lines. At the start of each line there are some numbers which there is ";" between them. How can I delete these numbers and ";"? (I tested split to put numbers together so can I delete them but when I use split, the word next to the numbers is put with them and when I delete the numbers, they delete, too. but I don't want to delete words. Just the the numbers and ";"). Or is there a way in notepad++?

The sample file: https://www.dropbox.com/s/yvgc659f9rrfhop/N.txt?dl=0

file = "c:/Python34/N.txt"
h = ["1","2","3","4","5","6","7","8","9","0", ";"]

with open (file) as f:
    for line in f:
        for i in h:
            if i in line:
                line.replace(i, "")
                print (line)
with open ("new.txt", "w") as f2:
    f2.write(line)

Upvotes: 0

Views: 50

Answers (1)

PeteyPii
PeteyPii

Reputation: 379

Regular expressions can deal with this:

import re 

file = 'c:/Python34/N.txt'
with open(file) as f:
    contents = re.sub(r'\d+;', '', f.read())
with open('new.txt', 'w') as f2:
    f2.write(contents)

Upvotes: 1

Related Questions