csv.reader()返回一个reader对象,利用该对象遍历csv文件中的行。

从csv文件中读取的每一行都作为字符串列表返回。

示例:读取文件,计算平均身高。

import csv
with open('sample\\student.csv') as f:
        row = csv.reader(f, delimiter = ',')
        next(row)  #读取首行
        height=[]
        for r in row:
            height.append(float(r[2]))  #读取的字段均为str类型
s=sum(height)     #计算列表的合计值
average=round(s/len(height),2)   #保留小数点后2位

python读取CSV文件,csv.reader()

 

相关文章:

  • 2021-04-12
  • 2022-12-23
  • 2021-12-18
  • 2021-07-27
猜你喜欢
  • 2022-12-23
  • 2021-06-02
  • 2021-11-13
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案