我试图找到/建立一个解决方案,但目前对我来说太复杂了。我有来自SAP的文本(存储在tkinter的文本中)
session.findById("wnd[0]").sendVKey 4
session.findById("wnd[1]/tbar[0]/btn[17]").press
session.findById("wnd[1]/usr/tabsG_SELONETABSTRIP/tabpTAB001/ssubSUBSCR_PRESEL:SAPLSDH4:0220/sub:SAPLSDH4:0220/ctxtG_SELFLD_TAB-LOW[3,24]").setFocus
session.findById("wnd[1]").sendVKey 0
session.findById("wnd[1]/usr/lbl[1,1]").setFocus
session.findById("wnd[1]").sendVKey 33
我想找出.sendVKey出现时的每一行,并将newtext放在行的开头(或行的上方)-例如,在示例中有3行:
在sendkey文本出现之前添加了新文本! session.findById(“wnd[0]”)。sendVKey 4
(……) 在sendkey文本出现之前添加了新文本!<;\b>; session.findById(“wnd[1]”)。sendVKey 0
(……)
尝试使用re.sub、re.findall和replace,但我认为这不是一个好方法。我的工作代码如下,但它不是动态的(在.sendVkey方法中,它可以是(“wnd[1]”中的不同窗口(0、1、2、3等)。有任何提示/解决方案吗?请帮助:(
def multiple_replace(string, rep_dict):
pattern = re.compile("|".join([re.escape(k) for k in sorted(rep_dict, key=len, reverse=True)]), flags=re.DOTALL)
return pattern.sub(lambda x: rep_dict[x.group(0)], string)
date_div = RPAcode.get("1.0", tk.END)
delay_vba_function = " Added new text before sendkey text occurs"
"
replaced_text = multiple_replace(date_div, {'.sendVKey' : '\n' + delay_vba_function + '\n' + '.sendVKey', \
'session.findById("wnd[1]").sendVKey' : '\n' + delay_vba_function + '\n' + 'session.findById("wnd[1]").sendVKey', \
'session.findById("wnd[2]").sendVKey' : '\n' + delay_vba_function + '\n' + 'session.findById("wnd[2]").sendVKey', \
'session.findById("wnd[3]").sendVKey' : '\n' + delay_vba_function + '\n' + 'session.findById("wnd[3]").sendVKey'})
#print(replaced_text)
RPAcode.delete('0.0', tk.END) #Deletes previous data
RPAcode.insert(1.0, replaced_text)
检查子字符串是否在字符串中的最简单方法是使用
in
-运算符(至少在Python3.5中是这样)。考虑以下事项:如果在终端中运行,输出将是:
因此,如您所见,脚本甚至在文本行前面添加了一些文本。如果您没有说
text = '(...)' + text
,而是说text = '(...)\n' + text
,那么(...)
将显示在实际文本上方的一行中。如果将上述代码示例放入一个for循环中,循环遍历所有文本行,我认为这种方法可能会解决您的问题编辑:
在遍历行之前,首先必须将文本拆分为行。我想这正是你需要的:
结果将是:
注意,如果您想要一个可伸缩的解决方案,我希望regex执行得更快(因为
for
-循环在Python中相对较慢)。但对于大多数中等规模的应用程序来说,这就足够了相关问题 更多 >
编程相关推荐