我设置了一个按钮来处理组合框中的两种可能性,这样如果选择了事件1,它将保存到第1到第4行,它可以正常工作并且保存没有问题但是当选择事件2时它应该保存5到8但是它没有正常工作5将把自己置于校正的线上,但是6会像10行一样下降,我昨天花了很多时间来添加和删除代码但是没有任何金牌。
def saveteam():
with open("data/tournamentdatae1.txt",'r') as f:
get_all=f.readlines()
event = combo_event.get()
if event == 'Event 1':
with open("data/tournamentdatae1.txt",'r+') as f:
for i,line in enumerate(get_all,1):
if i == 1:
input1 = team_1.get()
f.writelines(input1)
f.writelines("\n")
for i,line in enumerate(get_all,1):
if i == 2:
input2 = team_2.get()
f.writelines(input2)
f.writelines("\n")
for i,line in enumerate(get_all,1):
if i == 3:
input3 = team_3.get()
f.writelines(input3)
f.writelines("\n")
for i,line in enumerate(get_all,1):
if i == 4:
input4 = team_4.get()
f.writelines(input4)
f.writelines("\n")
if event == 'Event 2':
with open("data/tournamentdatae1.txt",'r+') as f:
for i,line in enumerate(get_all,1):
if i == 5:
input1 = team_1.get()
f.writelines(input1)
f.writelines("\n")
else:
f.writelines(line)
for i,line in enumerate(get_all,1):
if i == 6:
input2 = team_2.get()
f.writelines(input2)
f.writelines("\n")
else:
f.writelines(line)
这是我的工作,但不知道如何使用此
进行演出 if event == 'Event 1':
if indivcombo == 'Individual 1':
get_all[20] = individual.get() + "\n"
if indivcombo == 'Individual 2':
get_all[21] = individual.get() + "\n"
if indivcombo == 'Individual 3':
get_all[22] = individual.get() + "\n"
if indivcombo == 'Individual 4':
get_all[23] = individual.get() + "\n"
if indivcombo == 'Individual 5':
get_all[24] = individual.get() + "\n"
if indivcombo == 'Individual 6':
get_all[25] = individual.get() + "\n"
if indivcombo == 'Individual 7':
get_all[26] = individual.get() + "\n"
if indivcombo == 'Individual 8':
get_all[27] = individual.get() + "\n"
if indivcombo == 'Individual 9':
get_all[28] = individual.get() + "\n"
if indivcombo == 'Individual 10':
get_all[29] = individual.get() + "\n"
if indivcombo == 'Individual 11':
get_all[30] = individual.get() + "\n"
if indivcombo == 'Individual 12':
get_all[31] = individual.get() + "\n"
if indivcombo == 'Individual 13':
get_all[32] = individual.get() + "\n"
if indivcombo == 'Individual 14':
get_all[33] = individual.get() + "\n"
if indivcombo == 'Individual 15':
get_all[34] = individual.get() + "\n"
if indivcombo == 'Individual 16':
get_all[35] = individual.get() + "\n"
if indivcombo == 'Individual 17':
get_all[36] = individual.get() + "\n"
if indivcombo == 'Individual 18':
get_all[37] = individual.get() + "\n"
if indivcombo == 'Individual 19':
get_all[38] = individual.get() + "\n"
if indivcombo == 'Individual 20':
get_all[39] = individual.get() + "\n"
if event == 'Event 2':
if indivcombo == 'Individual 1':
get_all[40] = individual.get() + "\n"
if indivcombo == 'Individual 2':
get_all[41] = individual.get() + "\n"
答案 0 :(得分:0)
如果我理解正确(大的话),那么你想用team_X
变量中的值替换文件中的某些行。你这样做,你把整个文件读到get_all
(一个不再连接到文件的列表),然后以r+
模式重新打开文件,然后迭代该列表(但不是文件!)直到你得到你要覆盖的行,然后写入新打开的文件。
这应该不起作用,因为新打开的文件仍然在第一行,即使您循环列表也是如此。
相反,我建议直接在列表中替换值,并使用'w'
覆盖整个文件。
def saveteam():
filename = "data/tournamentdatae1.txt"
with open(filename, 'r') as f:
get_all = f.readlines()
event = combo_event.get()
if event == 'Event 1':
get_all[0] = team_1.get() + "\n"
get_all[1] = team_2.get() + "\n"
get_all[2] = team_3.get() + "\n"
get_all[3] = team_4.get() + "\n"
if event == 'Event 2':
get_all[4] = team_1.get() + "\n"
get_all[5] = team_2.get() + "\n"
get_all[6] = team_3.get() + "\n"
get_all[7] = team_4.get() + "\n"
with open(filename, 'w') as f:
for line in get_all:
f.write(line)
您可以通过将要替换的行和相应的团队变量放入列表和列表的dict,然后压缩和进行压缩来进一步减少代码重复(特别是如果有更多"事件"像这些)循环这些列表:
def saveteam():
filename = "data/tournamentdatae1.txt"
with open(filename, 'r') as f:
get_all = f.readlines()
team_vars = [team_1, team_2, team_3, team_4]
lines = {"Event 1": [0, 1, 2, 3], "Event 2": [4, 5, 6, 7]}
event = combo_event.get()
for team, line in zip(team_vars, lines.get(event, [])):
get_all[line] = team.get() + "\n"
with open(filename, 'w') as f:
for line in get_all:
f.write(line)
注意:我没有测试任何一个。
通过文件阅读团队的分析。 (注意:我移动了列表并从方法中删除了它,因为它始终是相同的,这样它可以重复用于读写团队。)
team_vars = [team_1, team_2, team_3, team_4]
lines = {"Event 1": [0, 1, 2, 3], "Event 2": [4, 5, 6, 7]}
filename = "data/tournamentdatae1.txt"
def showteam():
with open(filename, 'r') as f:
get_all = f.readlines()
event = combo_event.get()
for team, line in zip(team_vars, lines.get(event, [])):
team.set(get_all[line].strip())