前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ValueError: invalid literal for int() with base 10: ‘abc‘

ValueError: invalid literal for int() with base 10: ‘abc‘

作者头像
狼啸风云
修改2022-09-02 20:07:27
1.9K0
修改2022-09-02 20:07:27
举报
文章被收录于专栏:计算机视觉理论及其实现

有时候需要用int()函数转换字符串为整型,但是切记int()只能转化由纯数字组成的字符串,如下例:

代码语言:javascript
复制
 Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
 Type "copyright", "credits" or "license()" for more information.
 >>> str1='214567'
 >>> str2=''
 >>> str3='fsdf214356'
 >>> int(str1)
 214567
 >>> int(str2)
 Traceback (most recent call last):
   File "<pyshell#4>", line 1, in <module>
     int(str2)
 ValueError: invalid literal for int() with base 10: ''#不可转换空字符串为整型
 >>> int(str3)
 Traceback (most recent call last):
   File "<pyshell#5>", line 1, in <module>
     int(str3)
 ValueError: invalid literal for int() with base 10: 'fsdf214356'#不可转换非纯数字组成的字符串

非纯数字组成的字符串强转为整型会报错:ValueError: invalid literal for int() with base 10

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/12/28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档