2

i have a text file with some site links.... i want to remove the string which is before the site name. here is the input file >> input.txt:

http://www.site1.com/
http://site2.com/
https://www.site3333.co.uk/
site44.com/
http://www.site5.com/
site66.com/

output file should be like:

site1.com/
site2.com/
site3333.co.uk/
site44.com/
site5.com/
site66.com/

here is my code:

bad_words = ['https://', 'http://', 'www.']

with open('input.txt') as oldfile, open('output.txt', 'w') as newfile:
    for line in oldfile:
        if not any(bad_word in line for bad_word in bad_words):
            newfile.write(line)
print './ done'

when i run this code then it totally remove the lines which containing bad_words

site44.com/
site66.com/

what should i do with code to get my specific result?

Ali1331
  • 117
  • 2
  • 9
  • related: [How to search and replace text in a file using Python?](http://stackoverflow.com/q/17140886/4279) – jfs Sep 17 '14 at 07:47

1 Answers1

4

Thanks all i have solved this... code should be:

fin = open("input.txt")
fout = open("output.txt", "w+")
delete_list = ['https://', 'http://', 'www.']
for line in fin:
    for word in delete_list:
        line = line.replace(word, "")
    fout.write(line)
fin.close()
fout.close()
print './ done'
Ali1331
  • 117
  • 2
  • 9