I have to write a program that takes a word from the user and replaces it if it is in the text file with another word given by the user. Then takes that input and writes it to a new file. The only problem is, I have to do it without using the built in replace() string function.

What I have so far:

input_data = open('input.txt','r')
output_data = open('output.txt','w')
x = input('Enter the word to be replaced:')
y = input('What should I replace all occururrences of ', x, 'with?')

for line in input_data.split():
    for word in line:
        if word in line:
            #Not sure what is supposed to go here without replace.
    output_data.write(line)
input_data.close()
output_data.close()

Any hints or suggestions would be immensly helpful.

Combination of lines 7 and 8 does not make sense as condition would allways be True as the word comes through iteration of line (and is a character not word).

Are you allowed built in find method?

We are allowed to use the find method, but I'm not sure how that works. I looked at it again, and this is the progress I've made:

input_data = open('input.txt','r')
output_data = open('output.txt','w')
for line in input_data:
    #word = line in input_data.split()
    for word in line:
        x = input('Enter the word to be replaced: ')
        y = input('What should I replace all occurrences of ', x, 'with?')    
        if word in line == x:
            x = y
    output_data.write(line)
input_data.close()
output_data.close()
print 'All occurrences of ' x , 'in input.txt have been replaced by ' y, 'in output.txt.'

I keep getting a syntax error of the 'x' in the final print statement.

Here is a session with the find() method

>>> s = '"Give me bacon and eggs", said the other man'  # (Hemingway)
>>> s.find("an")
15               # substring "an" found in position 15
>>> s[15:15+2]
'an'
>>> s.find("an", 15+2)
42               # after position 17, "an" found in position 42
>>> s[42:42+2]
'an'
>>> s.find("an", 42 + 2)
-1               # no occurrences after position 44

Think about the flow of logic: Why are you asking the user for a new pair of words on every line? Shouldn't you do that at the top, then just use them in the loop?

Here's a function that (almost) returns the line that is manipulated, but if there are multiple adjacent spaces in the line, they are replaced by one space, or if the line has indentation, it is lost.

def collapse_spaces(line):
    build = []
    for w in line.split():
        build.append(w)
    return ' '.join(build)

You should be able to modify this to get a 90% on the homework problem, maybe more if the grader misses the point about indentation. What they probably want you to use, though, is slice replacement, based on http://docs.python.org/library/stdtypes.html#str.find

If you aren't sure how find works, you should forget all the other parts of the program for the moment and just program various uses of find until you understand how that works. That will make programming the rest of the task easier.

griswolf approach or similar is necessary if you do not want to touch parts of words like 'the' inside 'mathematics' when replacing 'the' with 'a', otherwice find method is nicer as it does not touch whitespace. White space is anyway difficult to keep OK, if the file is not TAB-spaced, when replacing with different length word.

This is what I ended up doing:

input_data = open('input.txt','r')
output_data = open('output.txt','w')
x = str(raw_input('Enter the word to be replaced:'))
y = str(raw_input('What should I replace all occurrences of ' +x+' with?'))
for line in input_data:
    words = line.split()
    for word in words:  
        if word == x:
            word = y
            output_data.write(word)
            output_data.write(' ')
        else:
            word = word
            output_data.write(word)
            output_data.write(' ')
input_data.close()
output_data.close()
print 'All occurrences of',x ,'in input.txt have been replaced by',y, 'in output.txt.'
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.