Python | Word Swap

/ 10 Jul 2013 /
So right now i'm still learning python, and I've been practicing 'regex' for the past hour, and used the tenacious d song list file as an example, here's the code



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import re

def main():
    fh = open ('rotf.txt')
    v = input('What word would you like to change? ')
    b = input('What word would you like to swap it with? ')
    for line in fh:
        match = re.search(v, line)
        if match:
            print(line.replace(match.group(), b), end='')

main()
In the list there are songs consisting of the 'f' word (if you know what I mean) so i wanted to change the 'f' word, to 'caress', and this is what i got: 

Caress Her Gently
Hard Caressing
I Wanna Caress You
They Caressed Our Asses

(I found this very amusing)
 
Copyright © 2010 M(ath)+me, All rights reserved