Python写入文本文件字典显示错误信息 [英] Python Write to Text File Dictionary Shows incorrect information

查看:207
本文介绍了Python写入文本文件字典显示错误信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我目前正在测试我的代码,以查看其是否正常运行,因此消除了所有不一致之处.


I'm currently testing my code to see if it functions properly, and therefore remove any inconsistencies.

首先,代码如下:

user_name = str(input("What is your name?"))
last_name = str(input("What is your surname?"))
final_total = int(input("What is your total score?"))

with open('quiz_results.txt') as f:
    for line in f:
        name,val = line.split(":")
        user_scores[name].appendleft(int(val))

    with open("quiz_results.txt", "a+") as f:
        f.write('{}:{}\n'.format(user_name + last_name, final_total))
        print("complete.")

    with open('quiz_results.txt') as f:
        for line in f:
            name,val = line.split(":")
            user_scores[name].appendleft(int(val))

现在,对于测试数据,我为用户名添加了"Dave",为姓氏添加了"Johnson",每次访问的最终总数分别为70、80、90和100,因此我重复了该程序4次.

Now, for test data, I added 'Dave' for the user_name, 'Johnson' for the last_name and final totals were 70, 80, 90 and 100 for each go, so I repeated the program 4 times.

当我输入第一个结果70时,我会在文本文件中得到期望的结果:

When I type in the first result, 70, I get the result I expect in the text file:

DaveJohnson:70

在文本文件中,当我键入80作为结果(具有相同的名称)时,我通常会进入文本文件:

Unusually enough, in the text file, when I type in 80 for the result (with the same name) I get in the text file:

DaveJohnson:80
DaveJohnson:70
DaveJohnson:70

什么时候我应该得到:

DaveJohnson:80
DaveJohnson:70

当我输入90时,我进入了文本文件:

When I type in 90, I get in the text file:

DaveJohnson:90
DaveJohnson:70
DaveJohnson:70

什么时候我应该得到:

DaveJohnson:90
DaveJohnson:80
DaveJohnson:70

当我输入100时,我进入了文本文件:

And When I type in 100, I get in the text file:

DaveJohnson:100
DaveJohnson:70
DaveJohnson:70

但是我应该得到:

DaveJohnson:100
DaveJohnson:90
DaveJohnson:80

到底是怎么了? 更新已解决.

What on Earth is going wrong? Update It is all solved.

推荐答案

您正在读取quizresults.txt两次,而没有重置user_names的内容.因此,如果文件开头有用户名/分数对,则您会将分数读入用户名,然后再次读入. user_scores的内容看起来像

You're reading quizresults.txt twice without reseting the content of user_names. So if a user name/score pair is in the file at the start you read the score into user names, then read it in again. The content of user_scores looks like

开始:{'DaveJohnson':[]} #I'm guesing

在第一次运行的第一个程序段之后:{'DaveJohnson':[]}

after first block on first run: {'DaveJohnson':[]}

在第一次运行的第二个程序段之后:{'DaveJohnson':[0]}

after second block on first run: {'DaveJohnson':[0]}

在第一次运行的第三个程序段之后:{'DaveJohnson':[70]}

after third block on first run: {'DaveJohnson':[70]}

在第一次运行的最终程序段之后:{'DaveJohnson':[70]}

after final block on first run: {'DaveJohnson':[70]}

在第二次运行的第一个程序段之后:{'DaveJohnson':[70]}

after first block on second run: {'DaveJohnson':[70]}

在第二次运行的第二个程序段之后:{'DaveJohnson':[70]} (在这里,您将DaveJohnson:80附加到文件中

after second block on second run: {'DaveJohnson':[70]} (here you append DaveJohnson:80 to your file

在第二次运行的第三段之后[在这里您从文件中重新读取DaveJohnson:70并读取DaveJohnson:80]:`{'DaveJohnson':[70,70,80] 出于好奇,为什么您有一个脚本正在读取文件,将其追加,再次读取然后覆盖它?

after third block on second run [here you re-read DaveJohnson:70 from the file and read DaveJohnson:80] : `{'DaveJohnson':[70,70,80] Out of curiousity, why do you have one script that is reading to a file, appending to it, reading it again and then overwriting it?

这篇关于Python写入文本文件字典显示错误信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆