zoukankan      html  css  js  c++  java
  • ValueError: invalid literal for int() with base 10: 'abc'

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

     1 Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
     2 Type "copyright", "credits" or "license()" for more information.
     3 >>> str1='214567'
     4 >>> str2=''
     5 >>> str3='fsdf214356'
     6 >>> int(str1)
     7 214567
     8 >>> int(str2)
     9 Traceback (most recent call last):
    10   File "<pyshell#4>", line 1, in <module>
    11     int(str2)
    12 ValueError: invalid literal for int() with base 10: ''#不可转换空字符串为整型
    13 >>> int(str3)
    14 Traceback (most recent call last):
    15   File "<pyshell#5>", line 1, in <module>
    16     int(str3)
    17 ValueError: invalid literal for int() with base 10: 'fsdf214356'#不可转换非纯数字组成的字符串
     18 >>>

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

  • 相关阅读:
    寒假学习10
    寒假学习9
    寒假学习8
    寒假学期7
    寒假学习6
    寒假学习5
    寒假学习4
    Notification通知栏的使用
    Service的使用
    BroadcastReceive的使用
  • 原文地址:https://www.cnblogs.com/helloworldcc/p/9681546.html
Copyright © 2011-2022 走看看