Help with mass remove in text file
Jeremy Moles
jeremy at emperorlinux.com
Wed Jul 13 12:56:22 EDT 2005
On Wed, 2005-07-13 at 09:00 -0700, rorley at gmail.com wrote:
> I'm trying to open a text file, remove all instances of the words
> "f=x;" and "i=x;" where x can be any number 0-14. Also, I want to
> remove all { " or ) or ( or ' } each time one of those characters
> occurs respectively. This is what I've been able to piece together...
Does this do what you're wanting?
-----------------------------------------------
finds = ("{", "}", "(", ")")
lines = file("foo.txt", "r").readlines()
for line in lines:
for find in finds:
if find in line:
line.replace(find, "")
print lines
-----------------------------------------------
>
> import os, string
> x = ("f=;")
> y = ("i=;)
> inputFile = open('abcd.txt','r')
> data = inputFile.read()
> inputFile.close()
> search = string.find(data, x)
> if search >=1:
> data = data.replace(x)
> data = data.replace(y)
> outputFile = open('abcd.txt','w')
> outputFile.write(data)
> outputFile.close()
>
>
> This doesn't work, even to just remove "f=;". Any help would be great.
>
> Thanks,
> Reece
>
More information about the Python-list
mailing list