我目前正在处理CSV文件,而且我的代码已经碰壁了。我试图达到的目标是能够写出一个包含20行和10列的CSV文件,1包含随机名称,8包含随机整数,最后一个包含随机字母等级(I' ve)能够成功完成此步骤)。然后读取文件,对每行的整数(生成的所有随机整数)求和,找出所有8个分数(整数)的平均值,并根据平均值打印最终的字母等级,并将它们放在最后一个,空的,列和每行的结尾。我目前无法找到这样做的方法,是否有任何可用于此目的的命令或任何有用的提示或提示? 就目前而言,这已经达到了这一点。
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']
def ranint():
value = random.randint(60, 100)
return value
def ranname():
first = random.choice(fname)
last = random.choice(lname)
fname.remove(first)
lname.remove(last)
return ("; ".join([first, last]))
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()
with open('grades2.csv', 'r', newline='') as fh:
z = csv.reader(fh)
答案 0 :(得分:1)
继续代码的最后一部分,您可以尝试:
with open('grades2.csv', 'r', newline='') as fh:
z = csv.reader(fh)
next(z) #skip header
for row in z:
#slice the integer columns and convert to int
integers=[int(i) for i in row[1:9] ]
#find average
avg_integers=sum(integers)/len(integers)
print(avg_integers)
答案 1 :(得分:0)
您可以使用pandas -
import pandas as pd
df = pd.read_csv('grades2.csv')
df['Total'] = df.sum(numeric_only=True, axis=1)
df['Mean'] = df['Total']/8