3

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

2
  • could you show us a data sample from sap.txt Commented Nov 22, 2013 at 14:05
  • Hi, the problem is solved but sure, here you go: | '''Ikona''' | style="border-top:none;border-bottom:0.75pt solid #000000;border-left:none;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Znaczenie''' |- | style="border:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| [[Image:]] | style="border:none;padding-top:0cm;padding-bottom:0cm;padding- Commented Nov 23, 2013 at 18:20

2 Answers 2

5

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'
Sign up to request clarification or add additional context in comments.

Comments

1

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.