CSV文件只写标题

时间:2018-03-28 03:36:14

标签: python excel function csv blank-line

我正在尝试编写一个CSV文件,该文件采用随机名称,分数,字母等级并生成CSV文件。当我在PyCharm上运行时,我创建了这个,但是当我检查它生成的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']
grade = ['A','B','C','D','F']
def ranint():
    value = random.randint(60, 100)
    print(value)
def ranname():
    first = random.choice(fname)
    last = random.choice(lname)
    print(first,last)
    fname.remove(first)
    lname.remove(last)
def rangrade():
    fgrade = random.choice(grade)
    print(fgrade)

with open('grades.csv','w',newline='') as fp:
    a = csv.writer(fp,delimiter=',')
    data = [['Name', 'Assignment1', 'Assignment2', 'Assignment3', 'Assignment4', 'Quiz1', 'Quiz2', 'Quiz3', 'Quiz4', 'Total_Grade'],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), rangrade()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), rangrade()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), rangrade()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), rangrade()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), rangrade()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), rangrade()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), rangrade()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), rangrade()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), rangrade()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), rangrade()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), rangrade()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), rangrade()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), rangrade()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), rangrade()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), rangrade()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), rangrade()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), rangrade()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), rangrade()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), rangrade()],
        [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), rangrade()]]
    a.writerows(data)

2 个答案:

答案 0 :(得分:0)

您的功能不会返回任何内容,只会打印随机值。在每个函数的末尾添加{ "_id" : ObjectId("5abb086677d96df7015c56c6"), "theater" : "Madou", "movie" : { "moviePhoto" : [ "https://movies.yahoo.com.tw/x/r/w420/i/o/production/movies/March2018/GD9otOLc6d3eprWHC1Ve-2757x4000.jpg", "https://movies.yahoo.com.tw/x/r/w420/i/o/production/movies/March2018/MueV83b8cYC28yRbknIT-3254x4650.jpg" ], "releasedTime" : [ [ ISODate("2018-03-27T12:20:00.000Z"), ISODate("2018-03-27T14:30:00.000Z"), ISODate("2018-03-27T15:10:00.000Z"), ISODate("2018-03-27T16:40:00.000Z"), ISODate("2018-03-27T18:50:00.000Z"), ISODate("2018-03-27T21:00:00.000Z"), ISODate("2018-03-27T21:40:00.000Z") ], [ ISODate("2018-03-27T13:00:00.000Z"), ISODate("2018-03-27T17:20:00.000Z"), ISODate("2018-03-27T19:30:00.000Z") ] ], "enName" : [ "Pacific Rim Uprising", "Tomb Raider" ] } } 语句。在返回值之前,您必须将returnfirst合并,例如与last

join

答案 1 :(得分:0)

您的函数没有返回语句。

例如:

def ranname():
    first = random.choice(fname)
    last = random.choice(lname)
    print(first,last)
    fname.remove(first)
    lname.remove(last)

需要像:

def ranname():
    first = random.choice(fname)
    last = random.choice(lname)
    print(first,last)
    return (first, last)
    fname.remove(first)
    lname.remove(last)