在CSV文件的每行末尾添加项目

时间:2018-04-07 19:59:14

标签: python csv append rows writing

我正在尝试编写一个代码,该代码写入一个包含1个标题行和20个其他行的CSV文件,每个行包含以下内容:1个随机名称,8个等级[随机生成],1个最终等级来自其他8个随机等级。我已经阅读了文件并生成了平均最终成绩,并将其附加到空列表中,以便我可以将其重新输入到文件中,但我无法写入/追加它们进入文件。

我发现没有x.writecolumns(),我在尝试完成此步骤时遇到了很多麻烦。这是我到目前为止所拥有的。提前感谢任何人提出建议。

import random
import csv

fname = ['Red', 'Jose', 'Mikasa', 'Sakura', 'Naruto', 'Momoka', 'Zero', 'Ryu', 'Ken', 'Geralt', 'Greg', 'Mika', 'Momo',
     'Xenovia', 'John', 'Gary', 'Lance', 'Eren', 'Duke', 'Claire']
lname = ['Akerman', 'Haruno', 'Johnson', 'Rivia', 'Valentine', 'Redfield', 'Kennedy', 'Juarez', 'Jameson', 'Williams',
     'Miller', 'Davis', 'Thompson', 'Garcia', 'Remington', 'Hayabusa', 'Strife', 'Armstrong', 'Kojima', 'Chambers']

avg_list = list() 
#^List where im appending all the average grades for all students

def ranint():
    value = random.randint(60, 100)
    return value
#^random value generator 

def ranname():
    first = random.choice(fname)
    last = random.choice(lname)
    fname.remove(first)
    lname.remove(last)
    return ("; ".join([first, last]))
#^picks a random first name and random last name

#CSV writer that has 1 header row and 20 other rows, each consisting of 1 name, 8 random grades, and 1 final grade averaged from the other 8
with open('grades2.csv', 'w', newline='') as fp:
a = csv.writer(fp, delimiter=',')
data = [['Name', 'If Else', 'Loops', 'Functions', 'File IO', 'Conditionals', 'Repetition', 'Python Functions',
         'Input Output', 'Grade'],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()]]
a.writerows(data)
fp.close()

#Reads the file, sums all 8 grades and gets the average. Does this for all students and then appends the results to a empty list
with open('grades2.csv', 'r+', newline='') as fh:
z = csv.reader(fh)
next(z)   #skip the header
for row in z:
    integers = [int(i) for i in row[1:9]]
    avg_integers = sum(integers) / len(integers)
    avg_list.append(avg_integers)
    print(avg_integers) #This is just here to check with the final CSV file
fh.close()

1 个答案:

答案 0 :(得分:0)

我相信你唯一的选择就是创建一个包含平均成绩的所有行数据的列表,然后清除文件的内容并重新写入csv。请参阅下面的代码来完成此任务:

import random
import csv

fname = ['Red', 'Jose', 'Mikasa', 'Sakura', 'Naruto', 'Momoka', 'Zero', 'Ryu', 'Ken', 'Geralt', 'Greg', 'Mika', 'Momo',
     'Xenovia', 'John', 'Gary', 'Lance', 'Eren', 'Duke', 'Claire']
lname = ['Akerman', 'Haruno', 'Johnson', 'Rivia', 'Valentine', 'Redfield', 'Kennedy', 'Juarez', 'Jameson', 'Williams',
     'Miller', 'Davis', 'Thompson', 'Garcia', 'Remington', 'Hayabusa', 'Strife', 'Armstrong', 'Kojima', 'Chambers']

avg_list = list() 
#^List where im appending all the average grades for all students

def ranint():
    value = random.randint(60, 100)
    return value
#^random value generator 

def ranname():
    first = random.choice(fname)
    last = random.choice(lname)
    fname.remove(first)
    lname.remove(last)
    return ("; ".join([first, last]))
#^picks a random first name and random last name

#CSV writer that has 1 header row and 20 other rows, each consisting of 1 name, 8 random grades, and 1 final grade averaged from the other 8
with open('C:/react/learnnextjs-demo/pages/grades2.csv', 'w', newline='') as fp:
    a = csv.writer(fp, delimiter=',')
    data = [['Name', 'If Else', 'Loops', 'Functions', 'File IO', 'Conditionals', 'Repetition', 'Python Functions',
             'Input Output', 'Grade'],
            [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
            [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
            [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
            [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
            [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
            [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
            [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
            [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
            [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
            [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
            [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
            [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
            [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
            [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
            [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
            [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
            [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
            [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
            [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
            [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()]]
    a.writerows(data)
    fp.close()

#Reads the file, sums all 8 grades and gets the average. Does this for all students and then appends the results to a empty list
with open('grades2.csv', 'r+', newline='') as fh:

    reader =  csv.reader(fh)
    next(reader)   #skip the header
    allRows = []
    for row in reader:
        integers = [int(i) for i in row[1:9]]
        avg_integers = sum(integers) / len(integers)
        row.append(avg_integers) #This is just here to check with the final CSV file
        allRows.append(row)
    fh.close()
    with open('grades2.csv', "w+", newline='') as fw:
        writer = csv.writer(fw)
        writer.writerows(allRows)
    fw.close()