python - 在 Python 中格式化输出文件
问题描述
我的程序输出一个 .nc 文件,这是一个纯文本 g 代码文件。当我打电话时
f.write(str(uniform))
该文件的格式如下:
("('G20G90M3S200G1X' 0.03125 'F20G1X' 0.0625 'F15G1X' 0.09375 'F10G1X' 0.125 'F20S300
如何修复我的代码,使输出文件的格式如下:
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)
. 以对您无用的方式创建数据是没有意义的。
推荐阅读
- reactjs - 对象不支持属性或方法“endsWith”,但我不使用它
- javascript - 如何从 FileReader() 输出值
- css - CSS Target 2nd td 仅在前 3 行
- html - ngModal 不适用于 ngFor 中的输入类型复选框
- angular - 模板引用变量在 ngFor 循环中未定义
- java - Mockito:使用列表
作为参数 - c++ - 为什么我会得到 std::exp 的特定于平台的结果?
- python - 连接到 sqlite 数据库时出现 sqlalchemy.exc.InvalidRequestError
- ruby-on-rails - 乘客 3.x 独立二进制文件连接超时
- python - 如何在张量流中制作自定义激活函数