如何以有组织的方式写入文件? [英] How to write to a file in an organized manner?

查看:21
本文介绍了如何以有组织的方式写入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

file = open('scores.txt','w')
playerscores = []
playernames = [['A'],['B'],['C'],['D'],['E'],['F']]   
for y in range(6):
    for z in range(5):
            print("Enter score from Judge",z+1,"for couple ",playernames[0+y],"in round 1:")
            playerscores.append(int(input()))    
MY_LIST = str(playerscores)
for_every = 4

本质上,我想通过在新行上打印以下索引位置来写入文件

Essentially, I want to write to the file by making the the following index positions print on a new line

playerscore[0:6]
playerscore[7:13]

所以它看起来像:

1,1,1,1,1,1,1,1,1,1

而不是 [1,1,1,1,1,1]

我需要这样做,所以当我这样做

I need to do this so when i do

file = open('filename','r')

并调用一个位置,它很容易给出.

and call a position, it is easily given out.

推荐答案

这里有一些代码可以满足您的需求,尽管它从我的 get_data() 函数而不是从 获取数据输入().这使得代码更容易测试.但是,一旦完成程序开发,您就可以轻松地将 get_data() 调用替换为 input().

Here's some code that does what you want, although it gets its data from my get_data() function instead of from input(). This makes the code easier to test. But you can easily replace the get_data() call with input() once you've finished developing the program.

关键思想是,除了将输入数据的整数版本保存到 playerscores 之外,我们还将其以原始字符串形式保存在名为 row 的单独列表中.因此,当我们完成读取给定行的数据后,我们可以轻松地将其保存到文件中.这比尝试从 playerscores 拆分数据并将其转换回字符串要简单.

The key idea is that as well as saving the integer version of the input data to playerscores we also save it in its original string form in a separate list named row. So when we've finished reading the data for a given row we can easily save it to the file. This is simpler than trying to split the data up from playerscores and converting it back into strings.

from random import seed, randrange

# Seed the randomizer
seed(42)

# Make some fake data, to simulate user input.
# Print & return a random number from 1 to 5, in string form
def get_data():
    n = str(randrange(1, 6))
    print(n)
    return n

playernames = ['A', 'B', 'C', 'D', 'E', 'F']

numjudges = 5

playerscores = []
scoresfile = open('scores.txt', 'w')

for players in playernames:
    row = []
    for z in range(1, numjudges + 1):
        print("Enter score from Judge", z, "for couple ", players, "in round 1:")
        data = get_data()
        playerscores.append(int(data))
        row.append(data)
    scoresfile.write(','.join(row) + '\n')
    print()
scoresfile.close()

典型输出

Enter score from Judge 1 for couple  A in round 1:
1
Enter score from Judge 2 for couple  A in round 1:
1
Enter score from Judge 3 for couple  A in round 1:
3
Enter score from Judge 4 for couple  A in round 1:
2
Enter score from Judge 5 for couple  A in round 1:
2

Enter score from Judge 1 for couple  B in round 1:
2
Enter score from Judge 2 for couple  B in round 1:
1
Enter score from Judge 3 for couple  B in round 1:
5
Enter score from Judge 4 for couple  B in round 1:
1
Enter score from Judge 5 for couple  B in round 1:
5

Enter score from Judge 1 for couple  C in round 1:
4
Enter score from Judge 2 for couple  C in round 1:
1
Enter score from Judge 3 for couple  C in round 1:
1
Enter score from Judge 4 for couple  C in round 1:
1
Enter score from Judge 5 for couple  C in round 1:
2

Enter score from Judge 1 for couple  D in round 1:
2
Enter score from Judge 2 for couple  D in round 1:
5
Enter score from Judge 3 for couple  D in round 1:
5
Enter score from Judge 4 for couple  D in round 1:
1
Enter score from Judge 5 for couple  D in round 1:
5

Enter score from Judge 1 for couple  E in round 1:
2
Enter score from Judge 2 for couple  E in round 1:
5
Enter score from Judge 3 for couple  E in round 1:
4
Enter score from Judge 4 for couple  E in round 1:
2
Enter score from Judge 5 for couple  E in round 1:
4

Enter score from Judge 1 for couple  F in round 1:
5
Enter score from Judge 2 for couple  F in round 1:
3
Enter score from Judge 3 for couple  F in round 1:
1
Enter score from Judge 4 for couple  F in round 1:
2
Enter score from Judge 5 for couple  F in round 1:
4

scores.txt 的内容

1,1,3,2,2
2,1,5,1,5
4,1,1,1,2
2,5,5,1,5
2,5,4,2,4
5,3,1,2,4

这篇关于如何以有组织的方式写入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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