简单的python程序阅读。gcode文件
我对Python完全陌生,我想创建一个简单的脚本,在文件中为我找到一个特定的值,然后用它执行一些计算 所以我有一个.gcode文件(对于一台3D打印机来说,这是一个有数千行的文件,但它可以被任何简单的文本编辑器打开)。我想创建一个简单的Python程序,当您启动它时,简单的GUI会打开,按钮会要求您选择一个.gcode文件。然后在文件被打开后,我想让程序找到具体的行 );所用灯丝=22900.5mm(55.1cm3) (以上是文件中行的精确格式)并从中提取值55.1。稍后我想用这个值做一些简单的计算。到目前为止,我已经制作了一个简单的GUI和一个带有“打开文件”选项的按钮,但是我一直在研究如何从这个文件中获取这个值作为一个数字(以便在以后的公式中使用) 我希望我已经解释清楚了我的问题,以便有人能帮助我:)提前谢谢你的帮助 到目前为止,我的代码是:简单的python程序阅读。gcode文件,python,file,search,g-code,Python,File,Search,G Code,我对Python完全陌生,我想创建一个简单的脚本,在文件中为我找到一个特定的值,然后用它执行一些计算 所以我有一个.gcode文件(对于一台3D打印机来说,这是一个有数千行的文件,但它可以被任何简单的文本编辑器打开)。我想创建一个简单的Python程序,当您启动它时,简单的GUI会打开,按钮会要求您选择一个.gcode文件。然后在文件被打开后,我想让程序找到具体的行 );所用灯丝=22900.5mm(55.1cm3) (以上是文件中行的精确格式)并从中提取值55.1。稍后我想用这个值做一些简单的
from tkinter import *
import re
# Here, we are creating our class, Window, and inheriting from the Frame
# class. Frame is a class from the tkinter module. (see Lib/tkinter/__init__)
class Window(Frame):
# Define settings upon initialization. Here you can specify
def __init__(self, master=None):
# parameters that you want to send through the Frame class.
Frame.__init__(self, master)
#reference to the master widget, which is the tk window
self.master = master
#with that, we want to then run init_window, which doesn't yet exist
self.init_window()
#Creation of init_window
def init_window(self):
# changing the title of our master widget
self.master.title("Used Filament Data")
# allowing the widget to take the full space of the root window
self.pack(fill=BOTH, expand=1)
# creating a menu instance
menu = Menu(self.master)
self.master.config(menu=menu)
# create the file object)
file = Menu(menu)
# adds a command to the menu option, calling it exit, and the
# command it runs on event is client_exit
file.add_command(label="Exit", command=self.client_exit)
#added "file" to our menu
menu.add_cascade(label="File", menu=file)
#Creating the button
quitButton = Button(self, text="Load GCODE",command=self.read_gcode)
quitButton.place(x=0, y=0)
def get_filament_value(self, filename):
with open(filename, 'r') as f_gcode:
data = f_gcode.read()
re_value = re.search('filament used = .*? \(([0-9.]+)', data)
if re_value:
value = float(re_value.group(1))
else:
print 'filament not found in {}'.format(root.fileName)
value = 0.0
return value
print get_filament_value('test.gcode')
def read_gcode(self):
root.fileName = filedialog.askopenfilename( filetypes = ( ("GCODE files", "*.gcode"),("All files", "*.*") ) )
self.value = self.get_filament_value(root.fileName)
def client_exit(self):
exit()
# root window created. Here, that would be the only window, but
# you can later have windows within windows.
root = Tk()
root.geometry("400x300")
#creation of an instance
app = Window(root)
#mainloop
root.mainloop()
您可以使用正则表达式在gcode文件中查找匹配行。下面的函数加载整个gcode文件并执行搜索。如果找到该值,该值将以浮点形式返回
import re
def get_filament_value(filename):
with open(filename, 'r') as f_gcode:
data = f_gcode.read()
re_value = re.search('filament used = .*? \(([0-9.]+)', data)
if re_value:
value = float(re_value.group(1))
else:
print('filament not found in {}'.format(filename))
value = 0.0
return value
print(get_filament_value('test.gcode'))
您的文件应显示以下内容:
55.1
因此,您的原始代码如下所示:
from tkinter import *
import re
# Here, we are creating our class, Window, and inheriting from the Frame
# class. Frame is a class from the tkinter module. (see Lib/tkinter/__init__)
class Window(Frame):
# Define settings upon initialization. Here you can specify
def __init__(self, master=None):
# parameters that you want to send through the Frame class.
Frame.__init__(self, master)
#reference to the master widget, which is the tk window
self.master = master
#with that, we want to then run init_window, which doesn't yet exist
self.init_window()
#Creation of init_window
def init_window(self):
# changing the title of our master widget
self.master.title("Used Filament Data")
# allowing the widget to take the full space of the root window
self.pack(fill=BOTH, expand=1)
# creating a menu instance
menu = Menu(self.master)
self.master.config(menu=menu)
# create the file object)
file = Menu(menu)
# adds a command to the menu option, calling it exit, and the
# command it runs on event is client_exit
file.add_command(label="Exit", command=self.client_exit)
#added "file" to our menu
menu.add_cascade(label="File", menu=file)
#Creating the button
quitButton = Button(self, text="Load GCODE",command=self.read_gcode)
quitButton.place(x=0, y=0)
# Load the gcode file in and extract the filament value
def get_filament_value(self, fileName):
with open(fileName, 'r') as f_gcode:
data = f_gcode.read()
re_value = re.search('filament used = .*? \(([0-9.]+)', data)
if re_value:
value = float(re_value.group(1))
print('filament value is {}'.format(value))
else:
value = 0.0
print('filament not found in {}'.format(fileName))
return value
def read_gcode(self):
root.fileName = filedialog.askopenfilename(filetypes = (("GCODE files", "*.gcode"), ("All files", "*.*")))
self.value = self.get_filament_value(root.fileName)
def client_exit(self):
exit()
# root window created. Here, that would be the only window, but
# you can later have windows within windows.
root = Tk()
root.geometry("400x300")
#creation of an instance
app = Window(root)
#mainloop
root.mainloop()
这将把结果作为一个浮点数保存到一个名为
value
的类变量中,谢谢您的快速回答!它看起来不错,我马上就用。我不确定一件事,但是,如何连接这个位,你写的文件,将上传时,你使用按钮。因为在这个程序中,不同的文件将使用不同的名称。因此,您编写的这个函数必须读取以前选择的文件。或者可能是这样,但我不明白?你传递了你想要的文件名,这样你就可以传递root.filename
例如。我用新的代码编辑了我的主要问题,我用你的建议更新了这些代码,但我得到了这样一条消息:“应该是缩进块”当我运行它时,代码中可能混合了制表符和空格,请从每行中删除所有前导字符并重新填充它们。另外,您可能希望通过添加self
作为第一个参数,并将其称为self.get\u value()
Ok,将我的函数更改为类的成员函数。我再次编辑了代码(更改在第一篇文章中),但仍然收到了此消息。我真的很抱歉,但我是Python(以及一般的编码)的超级初学者