代码之家  ›  专栏  ›  技术社区  ›  somebrick

如何有组织地写入文件?

  •  0
  • somebrick  · 技术社区  · 8 年前

    我有以下代码:

    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')
    

    2 回复  |  直到 8 年前
        1
  •  1
  •   PM 2Ring    8 年前

    这里有一些代码可以满足您的需要,尽管它是从我的 get_data() 函数而不是来自 input() get_data() 呼叫 一旦完成程序开发。

    关键思想是,除了将输入数据的整数版本保存到 playerscores 我们还将其以原始字符串形式保存在一个名为 row

    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
    
        2
  •  1
  •   coder    8 年前

    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