首页 > 解决方案 > 如何将一定范围的字符串存储到列表中

问题描述

我想要python文件,我想读取和捕获它的def和def本身的代码并存储到(containDEF1)列表中。如何捕获 def hello 及其内部的所有代码并将其存储到 (containDEF1) 列表中,然后将下一个 def hello2 捕获到代码内部并作为新元素存储到 (containDEF1) 中。并继续直到最后一个 def()。并忽略那些不在 def 块级别的。如果对如何实现这一目标有任何新想法,我会在下面列出我所做的事情,请告诉我谢谢。

蟒蛇文件:

#!C:\Python\Python39\python.exe
print ('Content-type: text/html\n\n')

    def hello(self):
        try:
            global testcaseResult
    
            assert self.Remedy.returnStatus
            assert self.Remedy.returnStatus
            print(self.Remedy.returnMessage)
    
        =self.MOSS IOS.element_wait_by_xpath_hide(self, mossDriver, action_msg, by_xpath, timeout=5)
        =self.Remedy.ResolutionCatPopulation(self, driver, ResoCat1, ResoCat2, ResoCat3=None)
    
    def hello2(self):
        try:
         global testcaseResult

         assert self.Remedy.returnStatus
         assert self.Remedy.returnStatus
         print(self.Remedy.returnMessage)
            =self.MOSS IOS.element_wait_by_xpath_get_text(self, mossDriver, action_msg, by_xpath, timeout=30)
    
    def hello3(self):
        =self.MOSS IOS.element_wait_by_xpath_get_text(self, mossDriver, action_msg, by_xpath, timeout=30)

print(funcs)
print(funcs)

我做了什么:

with open("test70.py", 'r') as inFile:
     funcs = inFile.read().split('\n\n')
     print(funcs)
     
    
     for elem in funcs:
      if elem.startswith('def'):
          containDEF1.append(elem)


print(containDEF1)

print(func) 的输出

["#!C:\\Users\\cxt764\\AppData\\Local\\Programs\\Python\\Python39\\python.exe\nprint ('Content-type: text/html\\n\\n')", 'def hello(self):\n\ttry:\n\t\tglobal testcaseResult', '\t\tassert self.Remedy.returnStatus\n\t\tassert self.Remedy.returnStatus\n\t\tprint(self.Remedy.returnMessage)', '\t=self.MOSS IOS.element_wait_by_xpath_hide(self, mossDriver, action_msg, by_xpath, timeout=5)\n\t=self.Remedy.ResolutionCatPopulation(self, driver, ResoCat1, ResoCat2, ResoCat3=None)', 'def hello2(self):\n\t=self.MOSS IOS.element_wait_by_xpath_get_text(self, mossDriver, action_msg, by_xpath, timeout=30)', 'def hello3(self):\n\t=self.MOSS IOS.element_wait_by_xpath_get_text(self, mossDriver, action_msg, by_xpath, timeout=30)', 'def hello4(self):\n\t=self.MOSS IOS.element_wait_by_xpath_get_text(self, mossDriver, action_msg, by_xpath, timeout=30)']

打印输出(包含DEF1):

['def hello(self):\n\ttry:\n\t\tglobal testcaseResult', 'def hello2(self):\n\t=self.MOSS IOS.element_wait_by_xpath_get_text(self, mossDriver, action_msg, by_xpath, timeout=30)', 'def hello3(self):\n\t=self.MOSS IOS.element_wait_by_xpath_get_text(self, mossDriver, action_msg, by_xpath, timeout=30)', 'def hello4(self):\n\t=self.MOSS IOS.element_wait_by_xpath_get_text(self, mossDriver, action_msg, by_xpath, timeout=30)']

问题:在def hello(self)缺少的部分代码'\t\tassert self.Remedy.returnStatus\n\t\tassert self.Remedy.returnStatus\n\t\tprint(self.Remedy.returnMessage)', '\t=self.MOSS IOS.element_wait_by_xpath_hide(self, mossDriver, action_msg, by_xpath, timeout=5)\n\t=self.Remedy.ResolutionCatPopulation(self, driver, ResoCat1, ResoCat2, ResoCat3=None)',中也应该在def hello中作为一个元素。

标签: python

解决方案


推荐阅读