【问题标题】:How to modify a file to replace a string that matches this pattern如何修改文件以替换与此模式匹配的字符串
【发布时间】:2013-10-10 23:36:38
【问题描述】:

我有一个这样的 json 文件:

{
    "title": "Pilot",
    "image": [
        {
            "resource": "http://images2.nokk.nocookie.net/__cb20110227141960/notr/images/8/8b/pilot.jpg",
            "description": "not yet implemented"
        }
    ],
    "content": "<p>The pilot ...</p>"
},
{
    "title": "Special Christmas (Part 1)",
    "image": [
        {
            "resource": "http://images1.nat.nocookie.net/__cb20090519172121/obli/images/e/ed/SpecialChristmas.jpg",
            "description": "not yet implemented"
        }
    ],
    "content": "<p>Last comment...</p>"
}

我需要替换文件中所有 resource 值的内容,所以如果 字符串具有以下格式:

"http://images1.nat.nocookie.net/__cb20090519172121/obli/images/e/ed/SpecialChristmas.jpg"

结果应该是:

"../img/SpecialChristmas.jpg"

有人可以告诉我如何匹配该模式以修改文件吗?

我尝试了类似这样的建议:

https://stackoverflow.com/a/4128192/521728

但我不知道如何适应我的情况。

提前致谢!

【问题讨论】:

  • 是否有任何非图像资源,或者它们都将是"../img/*"形式的图像?
  • 文件是否太大以至于不能只使用json.load 它,将其视为字典,然后json.dump 它?

标签: python regex file


【解决方案1】:

我会在组中使用正则表达式:

from StringIO import StringIO    
import re

reader = StringIO("""{
    "title": "Pilot",
    "image": [
        {
            "resource": "http://images2.nokk.nocookie.net/__cb20110227141960/notr/images/8/8b/pilot.jpg",
            "description": "not yet implemented"
        }
    ],
    "content": "<p>The pilot ...</p>"
},
{
    "title": "Special Christmas (Part 1)",
    "image": [
        {
            "resource": "http://images1.nat.nocookie.net/__cb20090519172121/obli/images/e/ed/SpecialChristmas.jpg",
            "description": "not yet implemented"
        }
    ],
    "content": "<p>Last comment...</p>"
}""")

# to open a file just use reader = open(filename)

text = reader.read()
pattern = r'"resource": ".+/(.+).jpg"'
replacement = '"resource": "../img/\g<1>.jpg"'
text = re.sub(pattern, replacement, text)

print(text)

解释模式。 "resource": ".+/(.+)?.jpg" :查找以"resource": " 开头的任何文本,然后在正斜杠之前有一个或多个字符,然后在.jpg" 之前有一个或多个字符。方括号() 表示我想要在其中找到的内容作为一个组。因为我只有一组括号,所以我可以用'\g&lt;1&gt;' 替换它。 (请注意,'\g&lt;0&gt;' 将匹配整个字符串:'"resources": etc'`)

【讨论】:

    【解决方案2】:

    如果都是"../img"中的图片,我相信你可以这样:

    resourceVal = "http://images1.nat.nocookie.net/__cb20090519172121/obli/images/e/ed/SpecialChristmas.jpg"
    lastSlash = resourceVal.rfind('/')
    result = "../img" + resourceVal[lastSlash:]
    

    如果还有其他类型的资源,这可能会稍微复杂一些 - 请告诉我,我会尝试编辑此答案以提供帮助。

    【讨论】:

      【解决方案3】:

      这是我的答案,不是很简洁,但您可以将re.search(".jpg",line) 行中使用的正则表达式调整为您想要的任何正则表达式。

      import re
      
      with open("new.json", "wt") as out:
      for line in open("test.json"):
          match = re.search(".jpg",line)
          if match:
            sp_str = line.split("/")
            new_line = '\t"resource":' + '"../img/'+sp_str[-1]
            out.write(new_line)
      
          else:
            out.write(line)
      

      【讨论】:

        猜你喜欢
        • 2021-01-26
        • 2020-07-20
        • 2012-09-26
        • 1970-01-01
        • 2013-08-13
        • 2014-03-22
        • 2016-06-17
        • 1970-01-01
        • 2020-10-13
        相关资源
        最近更新 更多