【发布时间】:2020-06-16 22:19:34
【问题描述】:
我试图找到/构建一个解决方案,但目前对我来说太复杂了。我有一个来自 SAP 的文本(存储在 tkinter 的 scrooledtext 中。
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 文本出现之前添加了新文本! 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)
【问题讨论】:
标签: python regex replace find findall