python - 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() 函数带来了利润。这些应该是数字。我能做些什么?
解决方案
for profit in row
实际上是循环遍历月份并在月份为字符串的情况下获利,因此出现错误。您可以尝试更换for profit in row
一个计数器来解决您的问题。计数器 i 和 j 用于指示您在哪个利润行。
beginning_balance = 0
并且ending_balance = 0
可以放在 之外for row in csvreader
以获得正确的计算net_change
和percent_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
推荐阅读
- mysql - mysql更新当前数据
- java - 为什么我不能再将布局设置为 GridBagLayout?
- c# - Trouble binding C# list to DropDownList in ASP.net
- paypal - PayPal IPN empty when buying by card
- c++ - Initializing variables in one/all processes in MPI
- javascript - 下载点击计数器 + Firebase 数据库
- javascript - 我如何与数组中的其余元素进行比较
- java - No suitable driver found for jdbc:sqlite on app image
- javascript - history.push 没有使用 ReactJs 重定向到特定页面
- scrapy - 如何将项目文件中的项目写入 csv,然后每次运行程序时附加 csv 文件