首页 > 解决方案 > CSV 文件问题。我转换为整数,但仍然得到 TypeError: unsupported operand type(s) for -: 'str' and 'str'

问题描述

执行涉及从 csv 文件读入的数据的计算时出现错误。我有一个包含 2 列的 csv 文件:日期和利润列。我必须在这个文件中添加两列来计算每月的净变化。由于读入数据是一个字符串,我确保将列包装在 int() 函数中并分配给一个变量。但是当我运行代码来计算值时,它仍然认为它是一个 str 类型。

我得到 TypeError: unsupported operand type(s) for -: 'str' and 'str'

with open(csv_path, 'r') as csvfile:
     csvreader = csv.reader(csvfile, delimiter =',')
     header = next(csvreader)
     
     # append 2 new columns to header row
     header.append('Net Change')
     header.append('Monthly Average Change')

     # loop through the csvfile row-by-row and convert columns. Only the profit column is number  data, I set variables equal to the row position and use numeric functions.
     
     for row in csvreader:
          month = row[0]
          profit = int(row[1])        # I wrap the int() function around this row to convert to int
          
          beginning_balance = 0
          ending_balance = 0

          for profit in row:
               if beginning_balance == 0:
                    beginning_balance = profit              # set the first $ value in file
                    ending_balance = profit                 

               elif beginning_balance != 0:
                   beginning_balance = ending_balance       # ending balance of prev month is new 
                   ending_balance = profit

               net_change = beginning_balance - ending_balance
               percent_change = net_change / beginning_balance

               # write the calculations to the added columns per row
               row.append(net_change)
               row.append(percent_change)

并且代码在 net_change = beginning_balance - ending_balance 部分失败。它认为我在数学计算中使用了字符串。我用 int() 函数带来了利润。这些应该是数字。我能做些什么?

标签: pythoncsvoperatorstypeerror

解决方案


for profit in row实际上是循环遍历月份并在月份为字符串的情况下获利,因此出现错误。您可以尝试更换for profit in row一个计数器来解决您的问题。计数器 i 和 j 用于指示您在哪个利润行。

beginning_balance = 0并且ending_balance = 0可以放在 之外for row in csvreader以获得正确的计算net_changepercent_change

更新代码如下。(=

with open(csv_path, 'r') as csvfile:
csvreader = csv.reader(csvfile, delimiter=',')
header = next(csvreader)

# append 2 new columns to header row
header.append('Net Change')
header.append('Monthly Average Change')

# loop through the csvfile row-by-row and convert columns. Only the profit column is number  data, I set variables equal to the row position and use numeric functions.
i = 0
j = 0
beginning_balance = 0
ending_balance = 0

for row in csvreader:
    month = row[0]
    profit = int(row[1])  # I wrap the int() function around this row to convert to int
    i += 1

    if j < i:
        if beginning_balance == 0:
            beginning_balance = profit  # set the first $ value in file
            ending_balance = profit

        elif beginning_balance != 0:
            beginning_balance = ending_balance  # ending balance of prev month is new
            ending_balance = profit

        net_change = beginning_balance - ending_balance
        percent_change = net_change / beginning_balance

        # write the calculations to the added columns per row
        row.append(net_change)
        row.append(percent_change)

    j += 1

推荐阅读