【问题标题】:CSV file issue. I converted to integer but still get TypeError: unsupported operand type(s) for -: 'str' and 'str'CSV 文件问题。我转换为整数,但仍然得到 TypeError: unsupported operand type(s) for -: 'str' and 'str'
【发布时间】:2021-11-12 18:24:38
【问题描述】:

执行涉及从 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() 函数带来了利润。这些应该是数字。我能做什么?

【问题讨论】:

  • 您后来使用for profit in row: 覆盖了profit = int(row[1])。不确定你在用 for 循环做什么,因为它正在遍历行的每一列,包括 row[0]row[1] 已经提取为 monthprofit 并且由于覆盖 profit将是一个字符串。

标签: python csv operators typeerror


【解决方案1】:

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

beginning_balance = 0ending_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

【讨论】:

    猜你喜欢
    • 2020-04-04
    • 2021-03-27
    • 2020-04-14
    • 2021-12-25
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多