decimal报错:decimal.InvalidOperation: [<class ‘decimal.ConversionSyntax‘>]

from decimal import *
sum_vol=0
sum_rev=0
sum_gp=0
list2=['5\n457.5\n363.4\n4\n1\n0.5\n-5,847.9', '900\n247.1\n-22.3\n800\n100\n24.7\n26.9']
for xx in list2[:]:
    x = xx.split('\n')
    x[0] = x[0].replace(',', '')
    x[3] = x[3].replace(',', '')
    x[4] = x[4].replace(',', '')
    # x[6] = x[6].replace(',', '')
    sum_vol += Decimal(x[-3])
    sum_rev += Decimal(x[-2])
    sum_gp += Decimal(x[-1])
print(sum_vol)
print(sum_rev)
print(sum_gp)

运行结果报错:
在这里插入图片描述
分析:
有3个求和,只有sum_gp报错了,原来是有个逗号
只需加上x[6] = x[6].replace(’,’, ‘’),就不会报错了

猜你喜欢

转载自blog.csdn.net/zhaoweiya/article/details/108797532