我目前正在编写一个程序来修改机器人的偏移值,这些值以文本形式存储在程序中。我已经设法完成了大部分功能,但我对最后一个功能有问题。它是re.sub公司方法。我一直收到一个错误消息:
raise TypeError(“第一个参数必须是string或compiled pattern”)
TypeError:第一个参数必须是string或compiled pattern。在
但是在我看来,第一个参数是一个编译模式。如果有人有什么建议的话,我可以帮你解决这个问题。这是有问题的部分。它的submit_x_offset函数。同样,is似乎删除了整个文件,同样的regex语句在find_x_offset中工作得很好,而当前还没有编译它。在
import os
import tkinter as tk
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
import re
class Controller(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.grid(row=0, column=0, columnspan=6, sticky=W)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (ChooseFile, GusToPlateRight, GusToPlateLeft):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky=NSEW)
self.show_frame(ChooseFile)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class ChooseFile(tk.Frame):
def open_file(self):
global filename
file.delete('1.0', END)
userentry_fname.delete('0', END)
f_types = [("FLW Programs", "*.ls"), ("Text Files ", "*.txt")]
filename = filedialog.askopenfilename(filetypes=f_types)
userentry_fname.insert(END, filename)
my_file = open(filename).read()
file.insert(END, my_file)
def clear_file(self):
file.delete('1.0', END)
userentry_fname.delete('0', END)
def find_x_offsets(self, xoffset_match):
# global matches
xoffset_match.delete('1.0', END)
with open(filename, 'r') as text:
string = text.read()
matches = re.search(
r'!GUSSET TO BACKPLATE RIGHT GUS 1.*PR\[GP1:2,1:OFFSET\]=([^; ]*)(?!ENDFOR).*ENDFOR.*', string,
re.M | re.DOTALL)
print(matches.group(1))
xoffset_match.insert(END, matches.group(1))
def submit_x_offsets(self, set_xoffset):
text_to_replace = set_xoffset.get()
offset_to_change = re.compile(
r'!GUSSET TO BACKPLATE RIGHT GUS 1.*PR\[GP1:2,1:OFFSET\]=([^; ]*)(?!ENDFOR).*ENDFOR.*', re.M | re.S).groups
with open(filename, 'r') as text:
string = text.read()
with open(filename, 'w') as file:
string = re.sub(offset_to_change, text_to_replace, string)
file.write(string.group(1))
我想我解释错了。我想把我在find_x_offset中搜索的同一行去掉。我需要把一个号码换成另一个号码。搜索功能我做得很好。没问题。我需要一个sub或replace函数
相关问题 更多 >
编程相关推荐