Python数据分析异常汇总
1. Insert Error: (1366, “Incorrect string value: ‘xF0x9Fx98x93’ for column ‘content’ at row 1”)
问题原因:mysql存储带有表情字符的内容(字符编码为utf8, 存储字段类型为longtext) 解决办法:mysql设置存储格式为varbinary(5000) 其他说明:查询解析的时候转换下格式即可 content.decode(utf8)
2. ‘NoneType’ object is not iterable
问题原因:返回的数据为None,使用时没做处理 解决办法:获取数据做is not None判断处理
if row is not None: c = row["content"] content = c if c is not None else
3. Select Error: tuple indices must be integers or slices, not str
问题原因:获取从数据库中数据解析错误,直接按json格式解析此处不合适 解决办法:content = row[2]
results = cursor.fetchall()
for row in results:
content = row[2] #解析查询返回数据行
print(content.decode(utf8))
4. TypeError: object of type ‘Response’ has no len()
res = requests.get(url, params=params, proxies=None) soup = BeautifulSoup(res.content, html.parser)
问题原因:res是requests对象,无法用BeautifulSoup解析, 解决办法:需要更改为res.content
5. TypeError: can only concatenate str (not “AttributeError”) to str
问题原因:print ("Select Error: " + err) 时候报出,类型不兼容不能组合。 解决办法:print ("Select Error: " + str(err))
以上
