2013-06-20 56 views
0

我有一個快速和骯髒的構建腳本,需要更新一個小的xml配置文件中的幾行。由於該文件是如此之小,我使用的是公認的低效的過程,以更新的地方文件只是爲了讓事情變得簡單:插入一個XML元素w/Python

def hide_osx_dock_icon(app): 
    for line in fileinput.input(os.path.join(app, 'Contents', 'Info.plist'), inplace=True): 
     line = re.sub(r'(<key>CFBundleDevelopmentRegion</key>)', '<key>LSUIElement</key><string>1</string>\g<1>', line.strip(), flags=re.IGNORECASE) 

    print line.strip() 

的想法是找到<key>CFBundleDevelopmentRegion</key>文字和插入前面的LSUIElement內容的權利的。我在另一個區域做了這樣的事情,它工作的很好,所以我想我只是缺少一些東西,但我沒有看到它。

我在做什麼錯?

+0

要打印只有最後一行,因爲'print'行沒有足夠的縮進。這是一個發佈錯誤? –

+0

Argh,no。不,這不是一個錯誤。好吧,無論如何,不​​要在帖子中。這是我無法看到的部分。如果你把這個作爲答案,我會把它標記爲答案。謝謝你的額外眼球。 –

+0

完成;在那裏,這樣做,橡皮鴨知道所有的祕密,並告訴任何人。 –

回答

0

你只打印出最後行,因爲你print聲明落在外面的for循環:

for line in fileinput.input(os.path.join(app, 'Contents', 'Info.plist'), inplace=True): 
    line = re.sub(r'(<key>CFBundleDevelopmentRegion</key>)', '<key>LSUIElement</key><string>1</string>\g<1>', line.strip(), flags=re.IGNORECASE) 

print line.strip() 

縮進該行以匹配以前:

for line in fileinput.input(os.path.join(app, 'Contents', 'Info.plist'), inplace=True): 
    line = re.sub(r'(<key>CFBundleDevelopmentRegion</key>)', '<key>LSUIElement</key><string>1</string>\g<1>', line.strip(), flags=re.IGNORECASE) 

    print line.strip()