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] 
相关推荐
摸鱼手会滑22 天前
源码编译安装python3.12没有ssl模块,python3.12 ModuleNotFoundError: No module named ‘_ssl‘
ssl·openssl·python3
刘悦的技术博客1 个月前
Python3.11二进制AI项目程序打包为苹果Mac App(DMG)-应用程序pyinstaller制作流程(AppleSilicon)
macos·ai·python3
la_vie_est_belle2 个月前
《Python爬虫逆向实战》绕过debugger的方法汇总
爬虫·python·python3·python爬虫逆向·过debugger
JavaPub-rodert2 个月前
【已解决】ERROR: No matching distribution found for torch.安装torch一次性解决方法
python·torch·pip·sd·python3·comfyui
shandianchengzi2 个月前
【代码】Python3|Scrapy框架初探(汽车之家大连市二手车车辆数据爬取、清洗与可视化)
爬虫·python·scrapy·汽车·python3
肖恭伟6 个月前
ubuntu install Python3
linux·python3
rs勿忘初心7 个月前
大模型快速实现python3+html内容在线渲染
前端·html·渲染·python3·代码
Yu_摆摆7 个月前
centos7中python3.10找不到openssl解决方案
centos7·openssl·python3
技术宅的宋哈哈8 个月前
Python爬虫采集下载中国知网《出版来源导航》PDF文档
爬虫·python·毕业设计·数据采集·python3·知网·中国知网