How to perform addition in text file bypassing strings

后端 未结 1 699
别跟我提以往
别跟我提以往 2021-01-28 10:19

I converted my csv file to text file and I want to add the numbers inside the text file. When I run my code get an error. Assuming the error code I want to write logic that woul

1条回答
  •  孤城傲影
    2021-01-28 11:03

    Modified your code as mentioned below:

      with open(r'annual_budget.txt', 'r') as f:
         reader = csv.reader(f)
         headers = reader.next() # this will yield first row i.e columns if python 3: Use next(reader) instead of reader.next()
         for line in reader:
             rowdata = map(float, line)
             data.extend(rowdata)
         print(sum(data)/len(data))
    

    0 讨论(0)
提交回复
热议问题