1

我有一个这样的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>"
}

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

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

结果应该是:

"../img/SpecialChristmas.jpg"

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

我试过这样的建议:

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

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

提前致谢!

4

3 回答 3

1

这是我的答案,不是很简洁,但是您可以将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)
于 2013-10-11T00:15:28.110 回答
1

如果它们都将成为图像中的图像"../img",我相信您可以这样做:

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

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

于 2013-10-11T00:08:01.650 回答
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<1>'. (请注意,这'\g<0>'将匹配整个字符串:'“资源”:等'`)

于 2013-10-11T02:09:25.313 回答