Skip to content Skip to sidebar Skip to footer

How To Write To A File In An Organized Manner?

I have this code: file = open('scores.txt','w') playerscores = [] playernames = [['A'],['B'],['C'],['D'],['E'],['F']] for y in range(6): for z in range(5): prin

Solution 1:

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.

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 from1to5, 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()

typical output

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

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

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

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

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

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

contents of 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

Solution 2:

If I understood correctly you are looking for something like this:

l = [4,3,2,5,6,4,6]
# split l in chunks -> e.g. [[4,3,2], [5,6,4,6]] 
chunks = [[4,3,2], [5,6,4,6]]

with open('file.txt', 'w') as f: 
    for i,chunk in enumerate(chunks):
        if i!=0:
            f.write('\n'+','.join(str(i) for i in chunk))
        else:
            f.write(','.join(str(i) for i in chunk))

# read data back in ls as integers
ls = []
with open('file.txt', 'r') as f:
    lines = f.read().splitlines()
    for line inlines:
        ls += map(int,line.split(','))

print ls

Post a Comment for "How To Write To A File In An Organized Manner?"