【问题标题】:How to change a string in a file to strings froma a list如何将文件中的字符串更改为列表中的字符串
【发布时间】:2013-12-07 10:08:05
【问题描述】:

我是 Python 编程的新手,我遇到了一个棘手的问题,即使用存储在列表中的字符串更改文本文件中的特定字符串。

我正在处理媒体 wiki 文档 - 将它们从 .doc 转换为 wiki 代码。转换后,所有图像都被标签 [[Media:]] 替换 - 我想用存储在列表中的图片名称替换所有 [[Media:]] 标签。例如,在转换后的文档中会有 5 个 [[Media:]] 标签,所以这意味着我有这样的列表:

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

我想将文档中的第一个标签更改为 bo 替换为 image1.jpg,在文档中找到的第二个标签替换为 image2.jpg 等等..

这是一段代码,但我不知道如何通过列表进行迭代工作

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

我会很感激你的帮助

【问题讨论】:

  • 你能给我们看一个来自sap.txt的数据样本
  • 您好,问题已解决,但可以确定,请执行以下操作:| '''伊科纳''' | 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;"| [[图片:]] | style="border:none;padding-top:0cm;padding-bottom:0cm;padding-

标签: python string file list


【解决方案1】:

如果你以后不需要使用li,你可以这样做:

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)

当然,如果文件包含的 "[[Media:]]" 实例多于 len(li) 实例,这会让您打开 IndexError

另外,如果你使用re,你可以用line=re.sub("\[\[Media:\]\]", lambda m: li.pop(), line)替换那个while循环。

【讨论】:

    【解决方案2】:

    一种简单但不是很有效的方法是一次替换一个标签:

    >>> 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'
    

    更好的方法是一次性构造字符串:

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

    像这样使用它:

    >>> 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'
    

    【讨论】:

      猜你喜欢
      • 2010-12-18
      • 2012-05-24
      • 1970-01-01
      • 2015-10-28
      • 2015-10-24
      • 2021-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多