Q. Write a program to read a file 'Story.txt' and create another file, storing an index of Story.txt, telling which line of the file each word appears in. If word appears more than once, then index should show all the line numbers containing the word.


Answer =


file1 = open("story.txt","r")
file2 = open("Path walla.txt","w")

data = file1.readlines()

for line in data :
    words = line.split()
    for j in words :
        file2.write( j )
        file2.write(" = ")
        file2.write( "index :- " + str ( line.index( j ) ) + " line:- " + str(data.index( line) +1 ) )
        file2.write( "\n" )

file1.close()
file2.close()

1 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post