python报错--ValueError: invalid literal for int() with base 10: ‘ ‘

2022-08-01,,,,

这些问题是在学习或项目中遇到的,现在进行总结。博主懒得举新的实例,就直接拿自己程序中的部分代码进行说明。理解这个意思就好,记录这个也是防止自己将来忘记。

ValueError: invalid literal for int() with base 10: ’ ’

翻译:ValueError:int()以10为底的无效文字:’’
实例:

               for i in range(3):
                    shendu = x[i][2:8]  # '{2000003}[0000000]' 
                    sd = int(shendu[-1])  # 3
                    shendu_data0 = list(x[i][2:7])  # ['0', '0', '0', '0', '0']
                    shendu_data0.insert(-sd, '.')  # ['0', '0', '0', '.', '0', '0']
                    shendu_data = ''.join(shendu_data0)  # 000.00
                    shendu_list.append(float(shendu_data))

报错信息:

原因:空字符串无法转成整型
解决办法:加入异常值抛出。若遇到ValueError错误,直接pass,否则执行else下面的程序。
修改后程序:

                for i in range(3):
                    # 数据处理
                    shendu = x[i][2:8]  # '{2000003}[0000000]'
                    try:
                        sd = int(shendu[-1])  # 3
                    except ValueError as e:
                        pass
                    else:
                        shendu_data0 = list(x[i][2:7])  # ['0', '0', '0', '0', '0']
                        shendu_data0.insert(-sd, '.')  # ['0', '0', '0', '.', '0', '0']
                        shendu_data = ''.join(shendu_data0)  # 000.00
                        shendu_list.append(float(shendu_data))

其他报错解决办法:
[python报错–AttributeError: ‘MainWindow‘ object has no attribute ‘setCentralWidget‘

本文地址:https://blog.csdn.net/zcs_xueli/article/details/107458068

《python报错--ValueError: invalid literal for int() with base 10: ‘ ‘.doc》

下载本文的Word格式文档,以方便收藏与打印。