我有一个像这样的 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
但我不知道如何使其适应我的情况。
提前致谢!
最佳答案
我将正则表达式与组一起使用:
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>'
将匹配整个字符串:'
"resources": etc'`)
关于python - 如何修改文件以替换与此模式匹配的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19307989/