[英]Formatting An Output File In Python
我的程序输出一个.nc 文件,这是一个纯文本 g 代码文件。 当我打电话
f.write(str(uniform))
该文件的格式如下:
("('G20G90M3S200G1X' 0.03125 'F20G1X' 0.0625 'F15G1X' 0.09375 'F10G1X' 0.125 'F20S300
如何修复我的代码,使 output 文件格式如下:
G20
G90
M3S200
G1X0.03125F20
G1X0.0625F15
G1X0.09375F10
G1X0.125F20S300
我添加了删除逗号的.replace(),但我不知道如何去掉单引号和双引号或空格,也不知道如何在需要的地方添加换行符。
这是我的代码:
from tkinter import *
from tkinter import ttk
import os
os.system('clear')
root = Tk()
root.title('Pickup Coil Turn Count Calculator')
app_width = 400
app_height = 600
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width / 2) - (app_width / 2)
y = (screen_height / 2) - (app_height / 2)
root.geometry(f'{app_width}x{app_height}+{int(x)}+{int(y)}')
root.configure(bg='light grey')
my_notebook = ttk.Notebook(root)
my_notebook.pack()
my_frame1 = Frame(my_notebook, width=400, height=600, bg='light grey')
my_frame2 = Frame(my_notebook, width=400, height=600, bg='light grey')
my_frame1.pack(fill='both', expand=1)
my_frame2.pack(fill='both', expand=1)
my_notebook.add(my_frame1, text='Turn Count Calculator')
my_notebook.add(my_frame2, text='G-Code Builder')
def turn_count():
width, length, ohms, resistance = float(e2.get()), float(e3.get()), float(options[clicked.get()]), float(e5.get())
turn_count = resistance / ohms * 1000 * 12 / ((width * 3.14) + length + length - width * .19) * .969
count_label['text'] = int(turn_count)
def no_scatter():
block1 = 'G20'\
'G90'\
'M3S200'\
'G1X', float(e1.get()) * .125, 'F20'\
'G1X', float(e1.get()) * .25, 'F15'\
'G1X', float(e1.get()) * .375, 'F10'\
'G1X', float(e1.get()) * .5, 'F20S300'\
'G1X', float(e1.get()) * .625, 'F10'\
'G1X', float(e1.get()) * .75, 'F10'\
'G1X', float(e1.get()) * .875, 'F15'\
'G1X', float(e1.get()), 'F20S600'\
'G1X', float(e1.get()) * .875, 'F20'\
'G1X', float(e1.get()) * .75, 'F15'\
'G1X', float(e1.get()) * .625, 'F10'\
'G1X', float(e1.get()) * .5, 'F10S1200'\
'G1X', float(e1.get()) * .375, 'F10'\
'G1X', float(e1.get()) * .25, 'F10'\
'G1X', float(e1.get()) * .125, 'F15'\
'G1X', '.000', 'F20'
block2 = 'G1X', float(e1.get()) * .125, 'F20'\
'G1X', float(e1.get()) * .25, 'F15'\
'G1X', float(e1.get()) * .375, 'F10'\
'G1X', float(e1.get()) * .5, 'F20'\
'G1X', float(e1.get()) * .625, 'F10'\
'G1X', float(e1.get()) * .75, 'F10'\
'G1X', float(e1.get()) * .875, 'F15'\
'G1X', float(e1.get()), 'F20'\
'G1X', float(e1.get()) * .875, 'F20'\
'G1X', float(e1.get()) * .75, 'F15'\
'G1X', float(e1.get()) * .625, 'F10'\
'G1X', float(e1.get()) * .5, 'F10'\
'G1X', float(e1.get()) * .375, 'F10'\
'G1X', float(e1.get()) * .25, 'F10'\
'G1X', float(e1.get()) * .125, 'F15'\
'G1X', '.000', 'F20'
uniform = str(block1), str(block2)
with open('No Scatter.nc', 'w') as f:
f.write(str(uniform).replace(',', ''))
myLabel = Label(my_frame1, text='Enter Core Height', bg='light grey')
myLabel.pack(pady=10)
e1 = Entry(my_frame1, width=10, justify='center', border=0)
e1.pack()
myLabel = Label(my_frame1, text='Enter Core Width', bg='light grey')
myLabel.pack(pady=10)
e2 = Entry(my_frame1, width=10, justify='center', border=0)
e2.pack()
myLabel = Label(my_frame1, text='Enter Core Length', bg='light grey')
myLabel.pack(pady=10)
e3 = Entry(my_frame1, width=10, justify='center', border=0)
e3.pack()
myLabel = Label(my_frame1, text='Enter Target Resistance In Ohms', bg='light grey')
myLabel.pack(pady=10)
e5 = Entry(my_frame1, width=10, justify='center', border=0)
e5.pack()
myLabel = Label(my_frame1, text='Select Wire Gauge', bg='light grey')
myLabel.pack(pady=10)
options = {
'38': '648.0',
'39': '846.0',
'40': '1079.0',
'41': '1323.0',
'42': '1659.0',
'43': '2143.0',
'44': '2593.0'
}
clicked = StringVar()
clicked.set(list(options.keys())[0])
drop = OptionMenu(my_frame1, clicked, *options.keys())
drop.pack()
Button(my_frame1, text='Calculate Turn Count', command=turn_count).pack(pady=30)
myLabel = Label(my_frame1, text='Turn Count', font='arial 14 bold', bg='light grey')
myLabel.pack()
count_label = Label(my_frame1, width=10, font='arial 20 bold', fg='white', bg='dark grey')
count_label.pack()
myLabel = Label(my_frame1, bg='light grey')
myLabel.pack()
myLabel = Label(my_frame2, text='Enter Winder Motor RPM', bg='light grey')
myLabel.pack(pady=10)
e6 = Entry(my_frame2, width=10, justify='center', border=0)
e6.pack()
myLabel = Label(my_frame2, bg='light grey')
myLabel.pack()
myLabel = Label(my_frame2, text='Select A Winding Pattern', bg='light grey')
myLabel.pack(pady=10)
Button(my_frame2, text='No Scatter', command=no_scatter).pack(pady=10)
Button(my_frame2, text='Minimum Scatter').pack(pady=10)
Button(my_frame2, text='Moderate Scatter').pack(pady=10)
Button(my_frame2, text='Maximum Scatter').pack(pady=10)
root.mainloop()
是你把它变成了一根大绳子。 如果您只是将其创建为字符串列表,那么您的问题将变得微不足道。
block1 = ['G20',
'G90',
'M3S200',
'G1X'+ str(float(e1.get()) * .125)+ 'F20',
'G1X'+ str(float(e1.get()) * .25)+ 'F15',
'G1X'+ str(float(e1.get()) * .375, 'F10',
'G1X'+ str(float(e1.get()) * .5)+ 'F20S300',
'G1X'+ str(float(e1.get()) * .625)+ 'F10',
...
一旦你有了它,你可以用'\n'.join(block1)
来写它。 以对您无用的方式创建数据是没有意义的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.