Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.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_Replace - Fatal编程技术网

python替代xml

python替代xml,python,replace,Python,Replace,我有一个名为FF_tuningConfig_AMPKi.xml的文件,其中包含以下记录: <KiConfig active="%{active}" id="AMP_RET_W_LIN_SUSPICIOUS_MULTIPLE_LOGIN_IN_SHORT_PERIOD$KiConfig"/> <KiConfig active="%{active}" id="AMP_RET_W_LIN_UNUSUAL_SESSION_HOUR_OF_DAY$KiConfig"/> 我有

我有一个名为FF_tuningConfig_AMPKi.xml的文件,其中包含以下记录:

<KiConfig active="%{active}" id="AMP_RET_W_LIN_SUSPICIOUS_MULTIPLE_LOGIN_IN_SHORT_PERIOD$KiConfig"/>
<KiConfig active="%{active}" id="AMP_RET_W_LIN_UNUSUAL_SESSION_HOUR_OF_DAY$KiConfig"/>

我有以下代码:

def replace_content(path,se,search,String_Replace):
    for root, dirs, files in os.walk(path):
        for filename in files:
            if((se in filename)):
                file=open(os.path.join(root, filename),'r')
                lines = file.readlines()
                file=open(os.path.join(root, filename),'w')

                for line in lines:
                    if search in line:
                    #print "found="+line
                        words=line.split('=')
                    #    print words
                     #   print "line=" + words[0] +"="+ "8\n" 
                        line=line.replace(line,String_Replace)
                    #print "after="+line
                    file.write(line)
                file.close()
                print (os.path.join(root,filename) + " was replaced")

replace_content(Path,'FF_tuningConfig_AMPKi.xml','<KiConfig active="%{active}"','<KiConfig active="true"')
def replace_内容(路径、se、搜索、字符串_replace):
对于os.walk(路径)中的根、目录和文件:
对于文件中的文件名:
如果((文件名中为se)):
file=open(os.path.join(根目录,文件名),'r')
lines=file.readlines()
file=open(os.path.join(根目录,文件名),'w')
对于行中的行:
如果在线搜索:
#打印“found=”+行
字=行。拆分('='))
#印刷文字
#打印“line=”+单词[0]+“=”+“8\n”
行=行.替换(行,字符串\u替换)
#打印“after=”+行
file.write(行)
file.close()文件
打印(os.path.join(根目录,文件名)+“已替换”)

替换内容(路径为'FF\u tuningConfig\u AMPKi.xml',“您的问题在于
line=line.replace(line,String\u replace)
。请查看相关文档


为了测试代码,您可以编写一个单独的脚本,其中只包含似乎失败的部分

# test input
s = '''<KiConfig active="%{active}" id="AMP_RET_W_LIN_SUSPICIOUS_MULTIPLE_LOGIN_IN_SHORT_PERIOD$KiConfig"/>
<KiConfig active="%{active}" id="AMP_RET_W_LIN_UNUSUAL_SESSION_HOUR_OF_DAY$KiConfig"/>'''

lines = s.split('\n')

# parameters
search, String_Replace = '<KiConfig active="%{active}"','<KiConfig active="true"'

# Then the part of your code that seems to be failing
for line in lines:
    if search in line:
        line = line.replace(line, String_Replace)
    print(line)
#测试输入
s='''
'''
lines=s.split(“\n”)
#参数

搜索,String_Replace='
我正在获取…
-这意味着什么?你的函数没有返回任何内容。这就是它正在打印的内容吗?@wwii大概就是OP在XML文件中获取的内容。你的问题可以简化为-打开然后读取文件;替换结果内容中的字符串;将修改后的内容写入文件。Pr问题在于修改内容。当你提出问题时,如果你向我们提供一个解决方案,这有助于我们集中精力。有时,当你为我们构建mcve时,这有助于你发现问题。你的问题不包含问题。你需要什么帮助?
<KiConfig active="true" id="AMP_RET_W_LIN_UNUSUAL_SESSION_HOUR_OF_DAY$KiConfig"/>
line = line.replace(search,String_Replace)
# test input
s = '''<KiConfig active="%{active}" id="AMP_RET_W_LIN_SUSPICIOUS_MULTIPLE_LOGIN_IN_SHORT_PERIOD$KiConfig"/>
<KiConfig active="%{active}" id="AMP_RET_W_LIN_UNUSUAL_SESSION_HOUR_OF_DAY$KiConfig"/>'''

lines = s.split('\n')

# parameters
search, String_Replace = '<KiConfig active="%{active}"','<KiConfig active="true"'

# Then the part of your code that seems to be failing
for line in lines:
    if search in line:
        line = line.replace(line, String_Replace)
    print(line)