How to change a string in a file to strings froma a list

I'am really new to Python programming and I have this burning problem with changing a specific string in a text file with a strings which are stored in a list.

I'am working on media wiki documents - converting them from .doc to wiki code. After a conversion all images are replaced by a tag [[Media:]] - I want to replace all [[Media:]] tags with a names of pictures stored in a list. For example in a converted document there will be 5 [[Media:]] tags so it means that I have list like this:

li = ["image1.jpg", "image2.jpg", "image3.jpg", "image4.jpg"]

And I want to change first tag in document to bo replaced with image1.jpg, the second tag found in document with image2.jpg and so on..

Here is a piece of code, but I can not figure how to make iteration through list to work

li = ["image1.jpg ", "image2.jpg ", "image3.jpg ", "image4.jpg ", "image5.jpg "]
a = 0

src = open('sap.txt').readlines()
dest = open('cel.txt', 'w')

for s in src:
    a += 1
    dest.write(s.replace("[[Media:]]", li[a]))
dest.close()

I will be grateful for help

Answers


A simple, but not very efficient way would be to replace the tags one at a time:

>>> li = ["image1.jpg", "image2.jpg", "image3.jpg", "image4.jpg"]
>>> s = "a tag [[Media:]] - I want to replace all [[Media:]] tags with a names of pictures stored in a list. For example in a converted document there will be 5 [[Media:]] tags d by a tag [[Media:]] - I want to replace all [[Media:]] tags"
>>> for item in li:
...    s = s.replace("[[Media:]]", item, 1)  # max. 1 replace per call
...
>>> s
'a tag image1.jpg  - I want to replace all image2.jpg  tags with a names of pictures stored in a list. For example in a converted document there will be 5 image3.jpg  tags d by a tag image4.jpg  - I want to replace all image5.jpg  tags'

A better way would be to construct the string in one go:

def interleave(original, tag, replacements):
    items = original.split(tag)
    return "".join((text+repl for text,repl in zip(items, replacements+[""])))

Use it like this:

>>> interleave(s, "[[Media:]]", li)
'a tag image1.jpg  - I want to replace all image2.jpg  tags with a names of pictures stored in a list. For example in a converted document there will be 5 image3.jpg  tags d by a tag image4.jpg  - I want to replace all image5.jpg  tags'

If you don't need to use li later, you could do:

li.reverse()
with open('sap.txt') as src:
    with open('cel.txt', 'w') as dest:
        for line in src:
            while "[[Media:]]" in line:
                line = line.replace("[[Media:]]", li.pop(), 1)
            dest.write(line)

Of course, this leaves you open to an IndexError if the file contains more instances of "[[Media:]]" than len(li).

Also, if you use re, you can replace that while loop with line=re.sub("\[\[Media:\]\]", lambda m: li.pop(), line).


Need Your Help

How do you support a web app with hashed or encrypted passwords?

security web-applications passwords credentials

When supporting a new web app in an enterprise environment, it is often necessary to log in as a specific user in order to diagnose a real or perceived problem they are having. Two opposing issues ...

Prototype.js erase select on jQuery trigger update

jquery select prototypejs conflict updates

I found a strange thing. On my website I need using both jQuery and Prototype.js, so I have jQuery in non-conflict mode and it works fine. But I have a jQuery script, that styles select boxes, and ...