Python TypeError: int() argument must be a string, a bytes原因

int()函数的TypeError

Python开发过程中,使用int()函数来转换或生成int类型的数据时,如果Python抛出并提示TypeError: int() argument must be a string, a bytes-like object or a real number, not 'complex',那么原因在于传递给int()函数的参数类型有误,正如TypeError的提示,int()函数的参数必须是string字符串(数值字符串)、类似字节对象、real number数字等,而不可以是complex复数类型的数据。

int()实例代码

python 复制代码
>>> str1 = '123'
>>> type(str1)
<class 'str'>
>>> int(str1)
123
>>> byteobj = b'56'
>>> type(byteobj)
<class 'bytes'>
>>> int(byteobj)
56
>>> realnumber = 3.12
>>> type(realnumber)
<class 'float'>
>>> int(realnumber)
3
>>> complexnum = 1+3j
>>> type(complexnum)
<class 'complex'>
>>> int(complexnum)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'complex'

原文:TypeError: int() argument must be a string, a bytes原因

免责声明:内容仅供参考!

相关推荐
dsyyyyy11012 分钟前
用JavaScript实现排序算法
开发语言·javascript·排序算法
m沐沐10 分钟前
【机器学习】基于 dlib 面部关键点的多表情分类
人工智能·python·深度学习·机器学习·计算机视觉·人脸识别·表情识别
CHANG_THE_WORLD12 分钟前
逐层拆解:C++ 虚函数从对象内存到手工调用的完整过程
java·开发语言·c++
Scott9999HH15 分钟前
2026 避坑实录:国产品牌压力变送器什么牌子好?从硬件抗扰到 C++ Qt 实时曲线绘制源码剖析
开发语言·c++·qt
geats人山人海30 分钟前
c# 第八章 多态与案例
开发语言·c#
️学习的小王33 分钟前
Python + MySQL 学生信息管理系统实战教程
android·python·mysql
lbb 小魔仙37 分钟前
从零搭建Python开发环境:Python安装 + VSCode + PyCharm 完整配置指南
vscode·python·pycharm
ch_atu39 分钟前
使用python下载modelscope上的模型
开发语言·python
spssau1 小时前
一文学会结构方程模型:从模型搭建、路径图绘制到不达标调整,实操全流程
人工智能·python·算法
Albart5751 小时前
Python爬虫请求头伪装后仍被反爬,基于代理池+随机延迟的绕过实战
开发语言·爬虫·python