Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.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 如何从文件中删除括号和括号内的内容_Python_Python 2.7 - Fatal编程技术网

Python 如何从文件中删除括号和括号内的内容

Python 如何从文件中删除括号和括号内的内容,python,python-2.7,Python,Python 2.7,我有一个名为sample.txt的文件,如下所示 ServiceProfile.SharediFCList[1].DefaultHandling=1 ServiceProfile.SharediFCList[1].ServiceInformation= ServiceProfile.SharediFCList[1].IncludeRegisterRequest=n ServiceProfile.SharediFCList[1].IncludeRegisterResponse=n 在这里,我的要

我有一个名为sample.txt的文件,如下所示

ServiceProfile.SharediFCList[1].DefaultHandling=1
ServiceProfile.SharediFCList[1].ServiceInformation=
ServiceProfile.SharediFCList[1].IncludeRegisterRequest=n
ServiceProfile.SharediFCList[1].IncludeRegisterResponse=n
在这里,我的要求是删除括号和整数,并用它输入os命令

ServiceProfile.SharediFCList.DefaultHandling=1
ServiceProfile.SharediFCList.ServiceInformation=
ServiceProfile.SharediFCList.IncludeRegisterRequest=n
ServiceProfile.SharediFCList.IncludeRegisterResponse=n 
我是Python的新手。这是我第一次尝试。我已使用这些代码删除括号:

#!/usr/bin/python
import re
import os
import sys
f = os.open("sample.txt", os.O_RDWR)
ret = os.read(f, 10000)
os.close(f)
print ret
var1 = re.sub("[\(\[].*?[\)\]]", "", ret)
print var1f = open("removed.cfg", "w+")
f.write(var1)
f.close()
在此之后,使用文件作为输入,我希望形成特定于应用程序的命令,如下所示:

cmcli INS  "DefaultHandling=1 ServiceInformation="
下一组是

cmcli INS "IncludeRegisterRequest=n IncludeRegisterRequest=y"
所以基本上现在我希望所有的输出都集中到一组两个,以便我在操作系统上执行命令


有什么方法可以将它们组合成一组两个吗?

如果文件是面向行的文本,并且也不可伸缩,那么将10000字节的文本读入字符串实际上是不必要的。您需要一个很好的理由来使用
os.open()
而不是
open()

因此,将数据视为文本行,每两行组成一行输出

from __future__ import print_function
import re

command = [None,None]
cmd_id = 1
bracket_re = re.compile(r".+\[\d\]\.(.+)")
# This doesn't just remove the brackets: what you actually seem to want is 
# to pick out everything after [1]. and ignore the rest.

with open("removed_cfg","w") as outfile:
    with open("sample.txt") as infile:
        for line in infile:
            m = bracket_re.match(line)
            cmd_id = 1 - cmd_id  # gives 0, 1, 0, 1
            command[cmd_id] = m.group(1)

            if cmd_id == 1: # we have a pair
                output_line = """cmcli INS "{0} {1}" """.format(*command)
                print (output_line, file=outfile)
这给出了输出

cmcli INS "DefaultHandling=1 ServiceInformation=" 
cmcli INS "IncludeRegisterRequest=n IncludeRegisterResponse=n" 
第二行与示例输出不对应。我不知道输入
IncludeRegisterResponse=n
应该如何变成输出
IncludeRegisterRequest=y
。我想那是个错误


请注意,此代码取决于您的输入数据是否与您描述的完全相同,并且没有任何错误检查。因此,如果输入的格式实际上比这更可变,那么您需要添加一些验证。

只有在您展示自己的代码尝试并用一个很好的包装解释您的困难所在之后。:)请分享你的工作,然后我们可以提出建议