【问题标题】:How to skip "ZeroDivisionError: float division by zero error" in python如何在python中跳过“ZeroDivisionError:浮点除以零错误”
【发布时间】:2020-07-11 16:38:11
【问题描述】:

我有一个 10 列的文件,我需要在 python 脚本中进行数学运算以进行文本处理。 我的脚本阅读

     prop=str(float(XX[4])*float(XX[4])*float(XX[5])*temp/float(XX[7]))

#Here XX[i's] 表示来自输入文件的列。 这给了我以下错误

ZeroDivisionError: float division by zero

对于类似的问题,有很多关于 SO 的答案。最适合我的是这个

https://stackoverflow.com/questions/53123918/strange-error-zerodivisionerror-float-division-by-zero

我试图按照上面链接中给出的答案作为

def percentage(aa[5],aa[7]):
    if aa[5] == 0:
        if aa[7] == 0:
            return float("nan")
        return float("inf")
return prop=str(float(XX[1])*float(XX[4])*float(XX[5])*temp/float(XX[7]))

但它给了我另一个错误

  def percentage(XX[5],XX[7]):
                     ^
SyntaxError: invalid syntax

我需要除法、道具、以太零或跳过给出零除法的计算。

我的文本文件是这样的

   0.05825   50.0000     17.99350445  0.00000000E+00  0.00000000E+00  0.00000000E+00  0.00000000E+00  0.00000000E+00  0.00000000E+00  0.00000000E+00
   0.05825  100.0000     17.99350445  0.13077009E-15  0.00000000E+00 -0.45510999E-18  0.00000000E+00  0.00000000E+00  0.81253965E-17  0.19522854E-26
   0.05825  150.0000     17.99350445  0.10397411E-08 -0.25009980E-02  0.17709387E+08 -0.37150035E+04  0.16657595E+05  0.33209360E-08  0.15522443E-19
   0.05825  200.0000     17.99350445  0.99342154E-06 -0.19166875E-02  0.20263129E+11 -0.29632045E+01  0.14942206E+08  0.24484631E-05  0.14830932E-16
   0.05825  250.0000     17.99350441  0.61349896E-04 -0.15611973E-02  0.14411398E+13 -0.39712767E-01  0.88306350E+09  0.12402531E-03  0.91590137E-15
   0.05825  300.0000     17.99350355  0.95878961E-03 -0.13214168E-02  0.25227492E+14 -0.21995273E-02  0.13338425E+11  0.16533485E-02  0.14313907E-13
   0.05825  350.0000     17.99349695  0.68254373E-02 -0.11478620E-02  0.19720032E+15 -0.27513111E-03  0.92363500E+11  0.10323277E-01  0.10189793E-12
   0.05825  400.0000     17.99346740  0.29726845E-01 -0.10156130E-02  0.92898089E+15 -0.57356824E-04  0.39294761E+12  0.40281504E-01  0.44379632E-12
   0.05825  450.0000     17.99337512  0.93350594E-01 -0.91087341E-03  0.31177821E+16 -0.16818798E-04  0.12085834E+13  0.11525763E+00  0.13936444E-11
   0.05825  500.0000     17.99315111  0.23327201E+00 -0.82544727E-03  0.82449358E+16 -0.62638878E-05  0.29628362E+13  0.26604916E+00  0.34825512E-11
   0.05830   50.0000     17.99350445  0.00000000E+00  0.00000000E+00  0.00000000E+00  0.00000000E+00  0.00000000E+00  0.00000000E+00  0.00000000E+00
   0.05830  100.0000     17.99350445  0.15031604E-15  0.00000000E+00 -0.53364087E-18  0.00000000E+00  0.00000000E+00  0.93211451E-17  0.22440897E-26
   0.05830  150.0000     17.99350445  0.10959274E-08 -0.24964636E-02  0.18666394E+08 -0.35245389E+04  0.17494317E+05  0.34874619E-08  0.16361258E-19

请帮帮我。

【问题讨论】:

  • def 期望名称在() 内提供参数。你的意思是def percentage(aa):?您没有指定要使用列表中的哪些元素。
  • 否,假设 def 将读取输入文件的第 5 列和第 7 列。我已经更新了问题。
  • 如果您只想传入某些列,则需要对集合进行索引以获取您想要的列,然后将这些列作为单独的参数传递给函数。

标签: python matplotlib


【解决方案1】:

基本上你得到的是:

try:
    1/0
except ZeroDivisionError:
    print('OOPS')
OOPS

为什么不呢:

try: 
   prop=str(float(XX[1])*float(XX[4])*float(XX[5])*temp/float(XX[7]))
except ZeroDivisionError:
    pass
    # Or prop = 0
    # Or some other action
    

更新。我无法复制语法错误:

try:
    prop=str(float(4)*float(3)*float(2)*5/float(0))
except ZeroDivisionError:
    prop = 0
    # test
prop
0

进一步更新

for x in qty:
          aa=x.split()
          if float(aa[1])==temp:
             first=str(float(aa[0])*ry_ev-ef)
             car_cm=str((float(aa[2])-cc_ef)/vol)
             pff=str(float(aa[4])*float(aa[4])*float(aa[5]))
             try:
                ztt=str(float(aa[4])*float(aa[4])*float(aa[5])*temp/float(aa[7]))
             except ZeroDivisionError:
                ztt=0
             finally:
                seeb.write(first  + "\t"  + aa[4]  + "\t"  + car_cm +  "\n")
                elc.write(first + "\t"  + aa[5]  + "\t"  + car_cm +  "\n")
                eth.write(first + "\t"  + aa[7]  + "\t"  + car_cm + "\n")
                pf.write(first  + "\t"  + pff    + "\t"  + car_cm + "\n")
                zt.write(first + "\t" + ztt +  "\n")
                dos.write(first + "\t" + aa[3] + "\n" )
                cc.write(first + "\t"  + car_cm + "\n")
                itr1+=1

这会产生:

Traceback (most recent call last):
  File "all-graphs.py", line 44, in <module>
    zt.write(first + "\t" + ztt +  "\n")
TypeError: can only concatenate str (not "int") to str

这是类型转换问题,您可以在异常中使用 ztt = "0" 或在 write() 中使用 str(ztt) 解决该问题。

【讨论】:

  • 我在 prop 文件之后添加了“除了 ZeroDivisionError: pass”并得到这个错误“除了 ZeroDivisionError: ^ SyntaxError: invalid syntax”
  • 见**更新**回答。我没有收到语法错误。您需要在问题中展示一个完整的示例,以便找出语法错误。
  • 抱歉,我错过了“try:”,因此出现语法错误。现在我收到另一个错误
  • 那会是什么?
  • IndentationError: 意外缩进
猜你喜欢
  • 2018-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多