解决Python的str强转int时遇到的问题

数字字符串前后有空格没事:

>>> print(int(" 3 "))
3

但是下面这种带小数点的情况是不可取的:

>>> print(int("3.0"))
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '3.0'

这种字符串强转float没问题

>>> print(float("3.0"))
3.0

以上这篇解决Python的str强转int时遇到的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

您可能感兴趣的文章:

  • python中将一个全部为int的list 转化为str的list方法
  • python 中的int()函数怎么用
(0)

相关推荐

  • python 中的int()函数怎么用

    int(x, [base]) 功能: 函数的作用是将一个数字或base类型的字符串转换成整数. 函数原型: int(x=0) int(x, base=10),base缺省值为10,也就是说不指定base的值时,函数将x按十进制处理. 适用Python版本: Python2.x Python3.x 注意: 1. x 可以是数字或字符串,但是base被赋值后 x 只能是字符串 2. x 作为字符串时必须是 base 类型,也就是说 x 变成数字时必须能用 base 进制表示 Python英文文档解释

  • python中将一个全部为int的list 转化为str的list方法

    假设有这样一个List [1,2,3,4,5] 转化为下面这个样子 ['1','2','3','4','5'] 解决方法一: a = [1,2,3] b = [ str(i) for i in a ] 解决方法二: >>> L = [1,2,3,4,5] >>> map(str, L) ['1', '2', '3', '4', '5'] 以上这篇python中将一个全部为int的list 转化为str的list方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也

  • 解决Python的str强转int时遇到的问题

    数字字符串前后有空格没事: >>> print(int(" 3 ")) 3 但是下面这种带小数点的情况是不可取的: >>> print(int("3.0")) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with b

  • 解决Python selenium get页面很慢时的问题

    driver.get("url")等到页面全部加载渲染完成后才会执行后续的脚本. 在执行脚本时,driver.get("url") ,如果当前的url页面内容较多加载特别慢,很费时间,但是我们需要操作的元素已经加载出来,可以将页面加载停掉,不影响后面的脚本执行,解决办法 设置页面加载timeout,get操作: try get except 脚本window.stop(), 使用GeckoDriver上有效果, 但是在ChromeDriver上还是会有问题,抛出异常

  • 解决python给列表里添加字典时被最后一个覆盖的问题

    如下所示: >>> item={} ; items=[] #先声明一个字典和一个列表,字典用来添加到列表里面 >>> item['index']=1 #给字典赋值 >>> items.append(item) >>> items [{'index': 1}] #添加到列表里面复合预期 >>> item['index']=2 #现在修改字典 >>> item {'index': 2} #修改成功 &g

  • 解决Python 异常TypeError: cannot concatenate 'str' and 'int' objects

    TypeError: cannot concatenate 'str' and 'int' objects print str + int 的时候就会这样了 python + 作为连接符的时候,不会自动给你把int转换成str 补充知识:TypeError: cannot concatenate 'str' and 'list' objects和Python读取和保存图片 运行程序时报错,然后我将list转化为str就好了. 利用''.join(list) 如果需要用逗号隔开,如1,2,3,4则

  • 解决Python中报错TypeError: must be str, not bytes问题

    如下所示: #!/usr/bin/python import pickle shoplist=['apple','mango','carrot'] f = open('c:\poem.txt','w') pickle.dump(shoplist,f) f.close() del shoplist f = open('c:\poem.txt','r') storedlist = pickle.load(f) print(storedlist) 执行上述程序时候报错: TypeError: must

  • 解决python 执行sql语句时所传参数含有单引号的问题

    在编写自己的程序时,需要实现将数据导入数据库,并且是带参数的传递. 执行语句如下: sql_str = "INSERT INTO teacher(t_name, t_info, t_phone, t_email) VALUES\ (\'%s\', \'%s\', \'%s\', \'%s\')" % (result, result2, phoneNumber, Email) cur.execute(sql_str) 执行程序后,产生错误: ProgrammingError: (1064

  • 解决Python 遍历字典时删除元素报异常的问题

    错误的代码① d = {'a':1, 'b':0, 'c':1, 'd':0} for key, val in d.items(): del(d[k]) 错误的代码② -- 对于Python3 d = {'a':1, 'b':0, 'c':1, 'd':0} for key, val in d.keys(): del(d[k]) 正确的代码 d = {'a':1, 'b':0, 'c':1, 'd':0} keys = list(d.keys()) for key, val in keys: d

  • Python中str is not callable问题详解及解决办法

    Python中str is not callable问题详解及解决办法 问题提出: 在Python的代码,在运行过程中,碰到了一个错误信息: python代码: def check_province_code(province, country): num = len(province) while num <3: province = ''.join([str(0),province]) num = num +1 return country + province 运行的错误信息: check

  • 解决python文件字符串转列表时遇到空行的问题

    文件内容如下: Alex 100000 Rain 80000 Egon 50000 Yuan 30000 #此处有一个空行! 现在看如何处理并转成列表! salary_info = open("salaryinfo.txt", "r+", encoding="UTF-8") salary_info_list = [] for line in salary_info.readlines(): if line == '\n': pass else:

随机推荐