[英]How can I use a str.replace to take a value from a spinbox, and then replace an instance of that value in a text file?
I am using the re module of python to find all instances of 2 patterns in a text file. 我正在使用python的re模块在文本文件中查找2个模式的所有实例。 I have a spinbox which returns the value of that which has been selected.
我有一个Spinbox,它返回已选择值的值。
from tkinter import *
import re
root = Tk()
num_names = open('numerical_sat_names.txt', 'r')
num_file = num_names.read()
satellite_TLE = open('TLE.txt', 'r')
TLE_file = satellite_TLE.read()
text = TLE_file
choice_var = tk.StringVar()
class FindTLE(tk.Frame):
pattern1 = r'(^.) (.....)(.) (........) (..)(............) (..........) (........) (........) (.) (....)(.$)'
pattern2 = r'(^.) (.....) (........) (........) (.......) (........) (........) (...........)(.....)(.$)'
multiline1 = re.compile(pattern1, re.MULTILINE)
multiline2 = re.compile(pattern2, re.MULTILINE)
print ('Multiline :')
for match in multiline1.findall(text):
print (' %r' % (match,))
if not pattern1:
raise TypeError('Bad line1 %s' % multiline1)
print ('Multiline2 :')
for match in multiline2.findall(text):
print (' %r' % (match,))
if not pattern2:
raise TypeError('Bad line2 %s' % multiline2)
def TLE_selected():
selected = str(choice_var.get())
print('selected :', selected)
str.replace('.....U', selected[1])
TLE_spinbox=tk.Spinbox(textvariable=choice_var, values=num_file,
command=TLE_selected)
TLE_spinbox.pack()
root.mainloop()
I would like the value of the spinbox to replace something within the text file; 我希望Spinbox的值可以替换文本文件中的内容; 'TLE.txt';
'TLE.txt'; that is, (.....U).
即(... U)。 However I get the typeError:
但是我得到typeError:
str.replace('.....U', selected[1])
TypeError: replace() takes at least 2 arguments (1 given)
The ('.....U') should correspond with part of 'pattern1' and follow the same structure as that which has been selected. ('... U')应该与'pattern1'的一部分相对应,并遵循与所选结构相同的结构。
I am quite new to python and therefore would appreciate some guidance as to what I am doing wrong, and how to go about solving this problem. 我是python的新手,因此希望对我做错了什么以及如何解决此问题提供一些指导。
replace can be used in two methods "Hello".replace("H", "h") which needs two arguments or str.replace("Hello", "H', "h") which needs 3 arguments - and here is your problem. replace可以在需要两个参数的两个方法“ Hello” .replace(“ H”,“ h”)或需要三个参数的str.replace(“ Hello”,“ H',” h“)中使用-这是你的问题。
(Comment by furas) (furas发表的评论)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.