替换文件中特定行的特定值

时间:2019-02-21 08:48:35

标签: python user-interface tkinter

我已经使用Tkinter创建了一个GUI。这是代码:

Test

在输入轴直径之后,我得到了眼睛的外径,叉的厚度等的某些值。我希望该值替换文件中存在的先前值。 考虑该文件是.txt文件,其中包含以下文本:

from tkinter import *

master = Tk()
def execute():
import os
os.chdir("batfile")
os.system("open.bat")

def calculations():
shaftdia = float(e1.get())
outdia = 2 * shaftdia
 thickfork = 0.75*shaftdia
 thicksingleye = 1.75*shaftdia
 diapin = shaftdia
 diapincollar = 1.5*shaftdia

 Label(master, text="{} ".format(str(outdia))).grid(row=5, column = 2, pady=5)
 Label(master, text="{} ".format(str(thickfork))).grid(row=6, column=2, pady=5)
 Label(master, text="{} ".format(str(thicksingleye))).grid(row=7, column=2, pady=5)
 Label(master, text="{} ".format(str(shaftdia))).grid(row=8, column=2, pady=5)
 Label(master, text="{} ".format(str(diapincollar))).grid(row=9, column=2, pady=5)

 print("Outer diameter of eye: %f" % float(outdia))
 print("Thickness of fork: %f" % float(thickfork))
 print("Thickness of single eye: %f" % float(thicksingleye))
 print("Diameter of pin: %f" % float(diapin))
 print("Diameter of knuckle pin and collar: %f" % float(diapincollar))
 pass


Label(master, text="Shaft Diameter").grid(row=4, pady=5)
Label(master, text="Outer diameter of eye").grid(row=5, pady=5)
Label(master, text="Thickness of fork").grid(row=6, pady=5)
Label(master, text="Thickness of single eye").grid(row=7, pady=5)
Label(master, text="Diameter of Pin").grid(row=8, pady=5)
Label(master, text="Diameter of knuckle pin head and collar").grid(row=9, pady=5)

 e1 = Entry(master)
 e1.grid(row=4, column=2)

Button(master, text='CALCULATE', command=calculations).grid(row=10, column=0)
Button(master, text='CREATE', command=execute).grid(row=10, column=1)

master.mainloop( ) 

我想用生成的新尺寸替换这些尺寸

2 个答案:

答案 0 :(得分:0)

考虑放置在txt文件中的值,需要进行更改。

list.txt:

Outer diameter of eye = 20
Thickness of fork = 7.5
Thickness of single eye = 17.5
Diameter of pin = 10
Diameter of knuckle pin and collar = 15

然后:

logFile = "list.txt"

with open(logFile) as f:
    content = f.readlines()

# you may also want to remove empty lines
content = [l.strip() for l in content if l.strip()]

# list of new Values
newVal_list = [23,54,67,19.43,4]
i = 0
for line in content:
    find_TC = line.find('=')

    if find_TC > 0:
        x = line.split('=', 1)[1]

        print(line.replace(x,str(newVal_list[i])))   # for replacing
        print("Old Value: {}, New Value: {}".format(x, newVal_list[i]))
        i += 1

输出

Old Value:  20, New Value: 23
Old Value:  7.5, New Value: 54
Old Value:  17.5, New Value: 67
Old Value:  10, New Value: 19.43
Old Value:  15, New Value: 4

编辑:

要使用fileinput搜索和替换同一文件中的值:

import fileinput

# list of new Values
newVal_list = [23,54,67,19.43,4]
i = 0
with fileinput.FileInput(logFile, inplace=True) as file:
    for line in file:
        find_TC = line.find('=')
        if find_TC > 0:
            x = line.split('=', 1)[1]
            line = line.replace(x, " " + str(newVal_list[i]))
            print(line)
            i += 1

输出

Outer diameter of eye = 23
Thickness of fork = 54
Thickness of single eye = 67
Diameter of pin = 19.43
Diameter of knuckle pin and collar = 4

答案 1 :(得分:0)

file = open("xyz.txt","w+")
file.write("") #whatever needs to be written
file.close()

open("xyz.txt","w+")要做的是创建一个名为xyz.txt的文件。

注意:如果文件不存在,则使用“ w +”,否则,如果文件存在,则可以使用“ w”。

每次运行代码时,文件中存在的值都会被删除,并且会向其中写入一组新值,从而替换旧值

如果文件已经创建,也可以使用

file = open("xyz.txt","w")

可以从here获得有关文件处理的更多信息。