2017-10-04 53 views
0

我有這樣的代碼:如何以有組織的方式寫入文件?

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 

從本質上講,我想通過使下面的指數位置上一個新行打印到寫入文件

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

所以它會看起來像:

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

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

我需要做,所以當我這樣做

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

,並調用的位置,很容易給出了。

+0

人想出了一個解決方案嗎? – somebrick

+0

爲什麼你將從input()得到的字符串轉換爲整數?你打算在以後的程序中跟他們做算術嗎? –

+0

您可以將當前行的輸入字符串保存到列表中,然後在'for z'循環完成後將它們連接到單個字符串中(使用編碼器答案中顯示的技術),因此您可以爲此寫入數據行到文件。 –

回答

1

下面是一些代碼,你想要做什麼,但它從我的get_data()功能,而不是從input()獲取數據。這使代碼更易於測試。但是,一旦完成開發程序,您可以輕鬆地用input()替換get_data()電話。

關鍵的想法是,除了將輸入數據的整數版本保存到playerscores之外,我們還將它以原始字符串形式保存在名爲row的單獨列表中。所以,當我們完成讀取給定行的數據時,我們可以輕鬆地將其保存到文件中。這比試圖從playerscores分割數據並將其轉換回字符串更簡單。 scores.txt的

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 

內容

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 
+0

這是完美的。非常感謝 – somebrick

+0

欣賞它。現在它變得更加容易了。 – somebrick

+0

@ PM2Ring不用擔心,這裏有一個+1 :) – coder

1

如果我理解正確,您正在尋找這樣的事情:

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 in lines: 
     ls += map(int,line.split(',')) 

print ls 
+0

謝謝你的作品,但我會如何去索引位置是像\ n – somebrick

+0

@Mahir什麼的?你的意思是 ?寫'1'是垂直的而不是水平的? – coder

+0

不喜歡,例如,如果我'playerscores = [4,3,2,5,6,4,6]''我想'playerscores [0:3]'與playercores [4: 5]' – somebrick