优化下列代码。keywords = ["CALL TIMER_START(1) ;", "CALL PROGRAM_ECHO(10) ;", 'CALL BG_CHECK ;', 'CALL MOVE_HOME ;', 'CALL MOVE_POUNCE ;', 'CALL SEGMENT(1) ;', 'CALL MOVE_HOME ;', 'CALL TIMER_STOP(1) ;'] # 修改string为更具体的语言描述需要检查的内容 string1 = "CALL TIMER_START(1) ;" string2 = "CALL PROGRAM_ECHO(10) ;" string3 = 'CALL BG_CHECK ;' string4 = 'CALL MOVE_HOME ;' string5 = 'CALL MOVE_POUNCE ;' string6 = 'CALL SEGMENT(1) ;' string7 = 'CALL MOVE_HOME ;' string8 = 'CALL TIMER_STOP(1) ;' unique_files = [] # 定义一个空的list num_unique_files = [] self.text_edit1.insert(tk.END, f'以下文本不符合点焊程序逻辑 \n') for root, dirs, files1 in os.walk(pns_folder): for filename in files1: file_path = os.path.join(root, filename) try: with codecs.open(file_path, 'r+', encoding='UTF-8') as f: lines = f.read().splitlines() except UnicodeDecodeError: with codecs.open(file_path, 'r+', encoding='GBK') as f: lines = f.read().splitlines() except IOError as error: messagebox.showerror('警告', file_path + '读取失败' + str(error)) order_correct = False index_list = [i for i, line in enumerate(lines) if any(keyword in line for keyword in keywords)]
时间: 2024-02-14 09:18:45 浏览: 21
下面是优化后的代码:
keywords = ["CALL TIMER_START(1) ;", "CALL PROGRAM_ECHO(10) ;",
'CALL BG_CHECK ;', 'CALL MOVE_HOME ;',
'CALL MOVE_POUNCE ;', 'CALL SEGMENT(1) ;',
'CALL MOVE_HOME ;', 'CALL TIMER_STOP(1) ;']
unique_files = []
num_unique_files = []
self.text_edit1.insert(tk.END, f'以下文本不符合点焊程序逻辑 \n')
for root, dirs, files1 in os.walk(pns_folder):
for filename in files1:
file_path = os.path.join(root, filename)
try:
with open(file_path, 'r+', encoding='UTF-8') as f:
lines = f.read().splitlines()
except UnicodeDecodeError:
with open(file_path, 'r+', encoding='GBK') as f:
lines = f.read().splitlines()
except IOError as error:
messagebox.showerror('警告', file_path + '读取失败' + str(error))
order_correct = False
index_list = [i for i, line in enumerate(lines) if any(keyword in line for keyword in keywords)]