python - 如何将一定范围的字符串存储到列表中
问题描述
我想要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中作为一个元素。
解决方案
推荐阅读
- regex - Flutter Dart:从字符串中提取 URL 的正则表达式
- node.js - 跨域设置cookies?节点和反应
- asp.net-core - 启动时加载资产文件失败,文件不存在
- pandas - 创建一个函数以从 pandas 中的数据框计算方程
- c++ - 没有重复的背包:最大数量的金币
- database - FSA 的 NoSQL 数据库设计
- javascript - 如何在进度条上悬停时生成视频缩略图并预览它们?
- python - 合并来自两个字典的数据框
- java - 使用 OrderBy 实现休眠
- spring-boot - 无法在应用程序属性中设置 cron 作业计划。SpringBoot ; 使用调度器在运行时调度 cron 作业