给定一个内容如下的文件:
{
"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"
from StringIO import StringIO
import re
import urllib
infile = open('test2.txt')
outfile = open('test3.txt', 'w')
pattern = r'"resource": ".+/(.+).jpg"'
replacement = '"resource": "../img/\g<1>.jpg"'
prog = re.compile(".+/(.+).jpg")
for line in infile:
if prog.match(line):
print (line) #this prints nothing
text = re.sub(pattern, replacement, line)
outfile.write(text)
infile.close()
outfile.close
但我也想打印循环中每个资源的值,如下所示:
"http://images1.nat.nocookie.net/__cb20090519172121/obli/images/e/ed/SpecialChristmas.jpg"
"http://images1.nat.nocookie.net/__cb20090519172121/obli/images/e/ed/SpecialChristmas.jpg"
我正在做的事情不起作用,那么在控制台中打印每个资源值的正确方法是什么?
提前致谢!