2013-10-10 28 views
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

,但我不知道如何使它適應我的處境。

在此先感謝!

+0

是否有任何非圖像資源,或者它們是否都是「../img/*」形式的圖像? '? –

+0

該文件是如此之大,它只是'json.load',禁止將它視爲字典,然後是'json.dump'嗎? – kojiro

回答

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>'會匹配整個字符串:'「resources」:etc'`)

1

如果他們都將是圖像"../img",我相信你能做到這一點是這樣的:

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

如果有其他類型的資源,這可能是一個有點複雜 - 讓我知道,我會嘗試編輯這個答案來幫助。

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) 
相關問題