1 TabError
TabError: inconsistent use of tabs and spaces in indentation
原因:代码中混用了tab和4个空格
解决:代码中全部使用tab或者全部使用4个空格
2 VSCode中使用opecv模块时报错
Module 'cv2' has no 'imread' member
原因:其实代码是没有错误的,可以正常运行,只是vscode的插件检测不到模块, 主要是因为cv2模块下还有cv2模块。
解决:在VSCode添加包的时候添加下面的语句
from cv2 import cv2
2 python replace函数替换无效问题
str = "hello,china!"
str.replace("hell","well")
print(str)
原因:
在Python中字符串是是不可变对象。
所以字符串使用replace需要重新赋值,生成一个新的对象。
之前没有重新引用,导致该变量 指向的是 以前的对象,实则已经发生变化,只是没有重新引用而已。
所以要想打印出替换后的字符串需要重新赋值,如下:
str = "hello,china!"
str = str.replace("hell","well")
print(str)