天使漫步IT工作室天使漫步IT工作室

TypeError: 'str' object does not support item assignment 解释及解决办法


Warning: count(): Parameter must be an array or an object that implements Countable in /www/wwwroot/u11u.com/usr/themes/wq/functions.php on line 110

Warning: count(): Parameter must be an array or an object that implements Countable in /www/wwwroot/u11u.com/usr/themes/wq/functions.php on line 116

解释:str 这个实例不支持或不能分配给item类型。item assignment 在python里面应该理解为可枚举的类型,比如list,和dict,set,duple等。

原因:

def add_fail_object(self,add_object,where_object):
        if add_object is None:
            return
        if int(self.failUrlsCol.find(where_object).count()) == 0:
            self.failUrlsCol.insert(add_object)

这是一段往mongodb插入数据集的一段代码,插入之前做了一个去重操作,也就是判断是否有重复内容。

插入代码:

key = "key"
url = "url"
self._db.add_object({"key":key,"url":url}, key)

所以在self.failUrlsCol.find(where_object).count()中抛出错误,后来回想mongdb实例中,查询是需要传入一个键值对类型的map,然而这里却传入了一个str,所以类型不匹配。

正确的办法应该是:where_object={"key",key} 即可。

本站原创,欢迎转载,转载敬请标明出处:天使漫步IT工作室 » TypeError: 'str' object does not support item assignment 解释及解决办法
添加新评论


Warning: Use of undefined constant php - assumed 'php' (this will throw an Error in a future version of PHP) in /www/wwwroot/u11u.com/usr/themes/wq/comments.php on line 38