简体   繁体   English

CSV文件的列数总和

[英]Sum of numbers of a column of CSV file

>The column has first OPEN in first error and then all the numbers(around 50 numbers for 50 different days) TypeError: 'float' object is not iterable --> strong text >该列的第一个错误是OPEN,然后是所有数字(50个不同日期的约50个数字)TypeError:“ float”对象不可迭代-> 强文本

import csv
with open("TCS_PAST.csv") as csvfile:
    readCSV = csv.reader(csvfile, delimiter = ',')
    next(readCSV)

    for row in readCSV:
        a = float(row[3].replace(",",""))
        print (a)
        print(sum(a)) -->

I can only guess what you are trying to accomplish here, but let me try: 我只能猜测您要在这里完成的工作,但让我尝试:

  • the commas are already removed by csv.reader() , no need in calling replace() 逗号已被csv.reader()删除,无需调用replace()

  • each line returned by csv.reader() is a list of strings which are floating numbers from your file. csv.reader()返回的每一行都是字符串列表,这些字符串是文件中的浮点数。 If you want to process elements starting with the third one you should write 如果要处理从第三个元素开始的元素,则应编写

    a = [float(x) for x in row[3:]] a = [第[3:]行中x的float(x)]

then a would be an array of floating numbers. 那么a将是一个浮点数数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM