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
Second, the formatting (whitespace) is all messed up on your post (at
least in googroups), so its not entirely clear what the program is
supposed to do.
Anyway, why do you say the program doesn't work? Does it generate an
error when you try to run it? Or is it doing what you say and not what
you mean?
I'm guessing that it will give (provided it doesn't end on a
SyntaxError) at least the following error:
TypeError: replace() takes at least 2 arguments (1 given)
This is because the line:
data = data.replace(x)
doesn't use enough arguments for the replace() method. You need to say
what to replace it with. Since you want to remove whatever string it
is, you can use the empty string as the replacement value:
data = data.replace(x, '') #That's two single quotes
Also, unless you want to make a regular expression, you might have to
do a replace() call on data 15 times, sort of like this:
for i in range(15):
text_to_remove = "f=%s;" % i
data = data.replace(text_to_remove, '')
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
-----------------------------------------------
See the thread titled "String Manipulation" which your classmate "jen"
started, which has several useful answers already.
Hopefully others will note that this is homework as well and not totally
hand you the answer on a platter, so you'll actually learn something.
Kudos to you for actually attempting it and posting your code though. ;-)
-Peter