Py学习  »  Graham Healy  »  全部回复
回复总数  1
6 年前
回复了 Graham Healy 创建的主题 » 带文本文件的python highscores

欢迎使用堆栈溢出!你用什么把你的高分存储在python中?一门课,一本字典?

要简单地将字符串内容写入文件,可以执行以下操作:

scores = [('username1', 123), ('username2', 456)]
with open('scores.txt', 'w') as f:
    for username, score in scores:
        f.write('Username: {0}, Score: {1}\n'.format(username, score))