部分报错处理记录

使用python3写代码执行时报错:AttributeError: 'list' object has no attribute 'replace'

报错原因:

list对象没有replace方法,str对象才有,在list对象上调用replace当然会报AttributeError,

报错写法:

c=b.replace

修改正确:

c=str(b).replace

python 2.7 无法升级pip至最新版本

Python 2.7已于2020年1月1日到期,请停止使用。请升级您的Python,因为不再维护Python 2.7。pip 21.0将于2021年1月停止对Python 2.7的支持。pip 21.0将删除对此功能的支持。解决此问题并继续使用不受支持的Python 2.7,不应升级到最新的pip版本,而应升级到<21的版本。

相关文档: https://pip.pypa.io/en/latest/development/release-process/#python-2-support

解决方法: 卸载python-pip后重新安装,升级时用以下指令:

pip install --upgrade "pip < 21.0"

pymongo连接报错

client = pymongo.MongoClient("172.23.210.21", 27017)
db = client.MonthlyTasks.get_collection('autohome_series')
items = db.find({"webstatus":"activated","getvehicles": {"$ne":now}},no_cursor_timeout=True)

python 使用pymongo库如果数据处理太慢可能会遇到类似如下的报错

pymongo.errors.CursorNotFound: cursor id 9136661443 not found

是因为跟数据库连接超时,加上.batch_size()参数,并指定一个较小的值,避免Python处理数据时间过长断开了跟数据库的连接db.find({"webstatus":"activated","getvehicles": {"$ne":now}}).batch_size(10)


Python3 使用.join时报错

使用[[Python3.6学习记录#列表转字符串,通过'' join来将列表转为字符串,前面的单引号中可以添加任何字符做转换后的分隔符,|''.join]]对列表内容处理时,如果列表内容由数值组成,则会报如下错误

TypeError: sequence item 0: expected str instance, int found

解决办法,将数值转换成字符串后再赋值给join 示例;

a = [1,2,3,4,5]
print(' ',join( str(i) for i in a ))

最后更新于