我正在尝试从csv文件中获取数据,将其打印出来并创建新文件。
from csv import*
from statistics import*
def read_csv(filename):
csv_data = []
fileObj = open("finals.csv", "r")
csvContents = reader( fileObj, delimiter =',')
for row in csvContents:
csv_data.append(row)
return csv_data
def print_table (data):
for i in data:
print(i)
def get_annual_attendance(data, year):
total = []
for i in data:
if i == year:
total.append[i]
return total
def get_statistics(data):
calculated_statistics = []
calculated_statistics.append(mean(data))
calculated_statistics.append(median(data))
calculated_statistics.append(stdev(data))
return calculated_statistics
def write_results(data, year, filename):
attendance = get_annual_attendance(data, year)
statistics = get_statistics(attendance)
csvFile = writer(filename, delimiter=',')
for entry in statistics:
csvFile.writerow(entry)
def main():
games = read_csv("finals.csv")
print_table(games)
write_results(games, "2009", "2009statistics.txt")
write_results(games, "2013", "2013statistics.txt")
write_results(games, "2018", "2018statistics.txt")
if __name__ == "__main__":
main()
这是我当前的代码。当我运行它时,它为我提供了我所需的大部分代码,但是却给了我一个错误。
Traceback (most recent call last):
File "C:/Users/brenn/Documents/python/Homework
#4/bouillion_brennan_hwf_nbafp.py", line 57, in <module>
main()
File "C:/Users/brenn/Documents/python/Homework
#4/bouillion_brennan_hwf_nbafp.py", line 51, in main
write_results(games, "2009", "2009statistics.txt")
File "C:/Users/brenn/Documents/python/Homework
#4/bouillion_brennan_hwf_nbafp.py", line 42, in write_results
statistics = get_statistics(attendance)
File "C:/Users/brenn/Documents/python/Homework
#4/bouillion_brennan_hwf_nbafp.py", line 34, in get_statistics
calculated_statistics.append(mean(data))
File "C:\Users\brenn\AppData\Local\Programs\Python\Python37-
32\lib\statistics.py", line 310, in mean
raise StatisticsError('mean requires at least one data point')
statistics.StatisticsError: mean requires at least one data point
而且我从未见过这种错误消息。感谢您的帮助。Correct output of the code