python3中split()函数和int()函数的坑

(一)

python3中的split()函数如果传入一个空字符,就会返回一个列表,该列表中有一个元素,此元素为空字符串。见如下代码,

python 复制代码
a = "".split(",")
for i,x in enumerate(a):
	print(f"i={i},x={x}.")

程序返回,

txt 复制代码
i=0,x=.

(二)

python3中的int()函数如果传入一个空字符,就会报错ValueError: invalid literal for int() with base 10: ''。参见如下代码,

python 复制代码
a = int("")

程序报错,

txt 复制代码
ValueError: invalid literal for int() with base 10: ''

(三)

综合上述,故可知,如下代码存在问题:

python 复制代码
a = ""
a = a.split(",")
a = [int(x) for x in a] 
相关推荐
一晌小贪欢2 天前
Python 测试利器:使用 pytest 高效编写和管理单元测试
python·单元测试·pytest·python3·python测试
编程火箭车8 天前
02.Win11 系统 Python 编程环境搭建实操指南(新手友好)
win11·python3·编程入门·环境变量配置·python 安装·新手友好·python 环境搭建
一晌小贪欢9 天前
深入解析 Python 3.11 版本迭代:性能飞跃与更优雅的错误处理
python·python基础·python3·python3.11·python小白
一晌小贪欢23 天前
Python 精确计算:告别浮点数陷阱,decimal 模块实战指南
开发语言·python·python入门·python3·python小数·python浮点数
xcLeigh1 个月前
python小游戏实战:打造一款视觉精美的飞机大战
开发语言·python·小游戏·pygame·python3·飞机大战
xcLeigh1 个月前
Python入门:Python3 数据结构全面学习教程
数据结构·python·学习·教程·python3
雪碧聊技术1 个月前
linux(centos7)如何执行python脚本
linux·python3·爬虫脚本·linux安装python3·linux运行爬虫脚本
一晌小贪欢1 个月前
【Python办公自动化】Python办公自动化常用库新手指南
开发语言·python·python自动化办公·python3·python办公自动化·python办公
一晌小贪欢2 个月前
Python-12 Python生成器与yield:惰性求值的艺术
开发语言·python·python基础·python3·python小白·python生成器
一晌小贪欢2 个月前
【Python办公】用 Selenium 自动化网页批量录入
开发语言·python·selenium·自动化·python3·python学习·网页自动化