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] 
相关推荐
xcLeigh17 小时前
Python 项目实战:用 Flask 实现 MySQL 数据库增删改查 API
数据库·python·mysql·flask·教程·python3
xcLeigh1 天前
Python入门:Python3 requests模块全面学习教程
开发语言·python·学习·模块·python3·requests
xcLeigh1 天前
Python入门:Python3 statistics模块全面学习教程
开发语言·python·学习·模块·python3·statistics
墨痕诉清风3 天前
CVS文件转Json格式
json·python3·cvs
一晌小贪欢8 天前
Python 测试利器:使用 pytest 高效编写和管理单元测试
python·单元测试·pytest·python3·python测试
编程火箭车15 天前
02.Win11 系统 Python 编程环境搭建实操指南(新手友好)
win11·python3·编程入门·环境变量配置·python 安装·新手友好·python 环境搭建
一晌小贪欢16 天前
深入解析 Python 3.11 版本迭代:性能飞跃与更优雅的错误处理
python·python基础·python3·python3.11·python小白
一晌小贪欢1 个月前
Python 精确计算:告别浮点数陷阱,decimal 模块实战指南
开发语言·python·python入门·python3·python小数·python浮点数
xcLeigh1 个月前
python小游戏实战:打造一款视觉精美的飞机大战
开发语言·python·小游戏·pygame·python3·飞机大战
xcLeigh1 个月前
Python入门:Python3 数据结构全面学习教程
数据结构·python·学习·教程·python3