Hello ! I am trying to make a little program, which would go through the string content, find some keywords inside of it and return their position in the text. Unfortunately my program does not work for the duplicated words like "AFIX" or "CONF" (see example bellow). Is there simple way to make this work for all words in a string ?
Thanks for help!

text='''ZERR
LATT 1
SYMM -X, 0.5+Y, 0.5-Z
SFAC C  H  N  O  P  Ni
UNIT 208 232 8 32 16 8
L.S. 3
AFIX 
AFIX
ACTA
BOND $H
FMAP 2
PLAN 20
SIZE 0.12 0.15 0.12
WPDB -1
CONF
CONF
TEMP -123.000
WGHT    0.044900
FVAR       0.05358
AFIX 
AFIX
AFIX
AFIX'''
listofwords=["CELL", "ZERR", "LATT", "END", "REM", "SYMM", "SFAC", "UNIT", "L.S.", "ACTA", "BOND $H", "FMAP", "PLAN", "SIZE", "WPDB", "CONF", "TEMP", "WGHT", "FVAR", "MOLE", "PART", "AFIX","ANIS", "WGHT"]
for i in listofwords:
            item = text.find(i)
            print item

For loop will only iterate one time over the list and not find duplicated words.

You can use text.find() method in a while loop.
This will iterate over all word in the text, when all search word are found text.find() will return -1.

Example.

text = """\
Hi,i have a nice car.
I drive to work every day in my car.
This text will find my car,....car.
"""

search_word = "car"
found = text.find(search_word)
while found > -1:
     print search_word, "found at index", found
     found = text.find(search_word, found + 1)

'''Out-->
car found at index 17
car found at index 54
car found at index 82
car found at index 90
'''
commented: Very clear answer +1

Thanks allot IT WORKS !!!!!!!!

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.