nish
nish

Reputation: 7280

Removing digits from a file

I have a file of the form:

car1 auto1 automobile1 machine4 motorcar1
bridge1 span5
road1 route2

But I want to remove the integers so that my file looks like:

car auto automobile machine motorcar
bridge span
road route

I am trying to read the file character by character, and if a character is a digit, skip it. But I am printing them in a new file. How can I make changes in the input file itself?

Upvotes: 3

Views: 11449

Answers (6)

Joshua Hickey
Joshua Hickey

Reputation: 1

I used this method for a million row dataset recently. Repeating yourself but quick and simple.

import regex as re

filename = r'/path/to/file/textfile.txt'

string = open(filename).read()

cleaned = re.sub('1', '', string)
cleaned = re.sub('2', '', cleaned)
cleaned = re.sub('3', '', cleaned)
cleaned = re.sub('4', '', cleaned)
cleaned = re.sub('5', '', cleaned)
cleaned = re.sub('6', '', cleaned)
cleaned = re.sub('7', '', cleaned)
cleaned = re.sub('8', '', cleaned)
cleaned = re.sub('9', '', cleaned)
cleaned = re.sub('0', '', cleaned)

open(filename, 'w').writelines(cleaned)

Upvotes: 0

Matthew Graves
Matthew Graves

Reputation: 3294

Using regular expressions:

import re
import fileinput

for line in fileinput.input("your_file.txt", inplace=True):
    print re.sub("\d+", "", line),

note: fileinput is a nice module for working with files.

Edit: for better performance/less flexibility you can use:

import fileinput
import string

for line in fileinput.input("your_file.txt", inplace=True):
    print line.translate(None, string.digits),

For multiple edits/replaces:

import fileinput
import re

for line in fileinput.input("your_file.txt", inplace=True):
    #remove digits
    result = ''.join(i for i in line if not i.isdigit())
    #remove dollar signs
    result = result.replace("$","")
    #some other regex, removes all y's
    result = re.sub("[Yy]+", "", result)
    print result,

Upvotes: 9

rumpelsepp
rumpelsepp

Reputation: 103

Use with to read/write the file and the str.translate function to replace the digits with an empty string. See here: http://docs.python.org/2/library/stdtypes.html#str.translate

with open('file', 'r') as f:
    data = f.read()
data = data.translate(None, '0123456789')
with open('file', 'w') as f:
    f.write(data)

Upvotes: 1

noisy
noisy

Reputation: 6823

with open('input.txt', 'r') as f1, open('output.txt', 'w') as f2:
    f2.write("".join([c for c in f1.read() if not c.isdigit()]))

Upvotes: 3

Joel Cornett
Joel Cornett

Reputation: 24788

with open('myfile.txt') as f:
    data = ''.join(i for i in f.read() if not i.isdigit())

with open('myfile.txt', 'w') as f:
    f.write(data)

Upvotes: 1

Ranjith Ramachandra
Ranjith Ramachandra

Reputation: 10774

fpath = '/path/to/your/file'
outpath = '/path/to/your/output/file'
f = open(fpath)
content = f.read()

new_content = ''

for letter in content:
    try:
        int(letter)
    except:
        new_content += letter

outf = open(outpath, 'w')
outf.write(new_content)
outf.close()
f.close()

Upvotes: 0

Related Questions