Python: replace keywords with values from the list in the order?

Hello.

There is a text file with the key word. Is approximately like this:

Sometext

Keyword

More sometext

Keyword

etc

There is also a list (text file) from which I want to replace key words. The key word is the same, the values for the substitutions are all different.

View the list of substitutions:

[attachmentid=1923106]
[attachmentid=1923108]
[attachmentid=1923110]
[attachmentid=1923112]
[attachmentid=1923114]

and so on.

I made the following script in Python (version 3.5.1):

list = []
with open('d:/attachment.txt','r') as inf:
 list = inf.read()
file = open('d:/new_1.txt', 'r')
text = file.read()
num_of_words = text.count('Image')
for i in range(0, num_of_words):
 text = text.replace('Image', 'list', 1)
file.close()
file = open('d:/new_1.txt', 'w')
file.write(text)
file.close()


attachment.txt - repository for a list of substitutions (attachmentid), new_1.txt file where change is made.

This code substitutes the entire list for each occurrence of the keywords. I would like to for the first occurrence of the keywords you inserted the first item of the list, second for second, etc:

Sometext

Keyword = [attachmentid=1923106]

More sometext

Keyword = [attachmentid=1923108]

etc

Please tell me how to fix these things.

PS don't know Python only a little less than completely. Code compiled with the help of Google and answers from StackOverflow.
July 4th 19 at 22:55
2 answers
July 4th 19 at 22:57
Solution
In the line
text = text.replace('Image', 'list', 1)

You need to add an index to each step were substituted only the element with that index, and not the entire list:

text = text.replace('Image', list[i], 1)

But this will work only if the number of replacement lines is guaranteed not less than the number of places for replacement. Otherwise will get an error.
With the addition of aRegius now everything works perfectly. Thank you very much!

And the number of occurrences of key words and the number of substitutions is tested separately. - Vaughn.Schaden commented on July 4th 19 at 23:00
July 4th 19 at 22:59
Solution
Hello.

Complement Alexanderthat list you have, most likely, will be a single string, like "mom soap frame"... But because it will need to break the split - list.split() - else list[0] returns only the first element of the string (in the example given by me the letter "m").

>>> text = "'
[attachmentid=1923106] 
[attachmentid=1923108] 
[attachmentid=1923110] 
[attachmentid=1923112] 
[attachmentid=1923114]
"'
>>> text[0]
'\n'
>>> text[1]
'['


>>> with new_text = text.split()
>>> with new_text[0]
'[attachmentid=1923106]'
>>> with new_text[1]
'[attachmentid=1923108]'
Just wanted to write that option Alexander takes the value of the replacement character, as pointed out by you and already changed the read method read() to readlines(), but this option added extra blank line in replacement. With your addition it works fine.

Thank you very much! - Vaughn.Schaden commented on July 4th 19 at 23:02
: Success! - Vaughn.Schaden commented on July 4th 19 at 23:05

Find more questions by tags ProgrammingPython