Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
插入带有Python的XML元素_Python_Regex_Xml_Python 2.7 - Fatal编程技术网

插入带有Python的XML元素

插入带有Python的XML元素,python,regex,xml,python-2.7,Python,Regex,Xml,Python 2.7,我有一个快速而肮脏的构建脚本,需要在一个小的xml配置文件中更新几行。由于文件太小,我使用一个公认的低效过程来更新文件,只是为了保持简单: 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>CFBundleDevelopmentRegi

我有一个快速而肮脏的构建脚本,需要在一个小的xml配置文件中更新几行。由于文件太小,我使用一个公认的低效过程来更新文件,只是为了保持简单:

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()
def hide_osx_dock_图标(应用程序):
对于fileinput.input(os.path.join(app,'Contents','Info.plist')中的行,inplace=True):
line=re.sub(r'(CbundleDevelopmentRegion)'LSUIElement1\g',line.strip(),flags=re.IGNORECASE)
打印行.strip()
这样做的目的是找到
CFBundleDevelopmentRegion
文本,并在其前面插入
LSUIElement
内容。我在另一个领域做了一些类似的事情,它工作得很好,所以我想我只是错过了一些东西,但我看不到

我做错了什么?

您只打印最后一行,因为您的
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()
用于fileinput.input(os.path.join(app,'Contents','Info.plist')中的行,inplace=True):
line=re.sub(r'(CbundleDevelopmentRegion)'LSUIElement1\g',line.strip(),flags=re.IGNORECASE)
打印行.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()
用于fileinput.input(os.path.join(app,'Contents','Info.plist')中的行,inplace=True):
line=re.sub(r'(CbundleDevelopmentRegion)'LSUIElement1\g',line.strip(),flags=re.IGNORECASE)
打印行.strip()

您只打印最后一行,因为
打印
行缩进不够远。这是一个发布错误吗?啊,不,不,这不是一个错误。嗯,反正也不在邮报上。这是我看不到的部分。如果你把这个作为答案,我会把它标记为答案。谢谢你的额外眼球。完成;在那里,做了那件事,橡皮鸭知道所有的秘密,不告诉任何人。