主要是为了备忘,熟悉
(1)配置powershell的环境变量
如果powershell中直接敲:python报错,那么环境变量配置路径如下
[Environment]::SetEnvironmentVariable("Path","$env:Path;D:\Python27","User")
(2) 代码里输入了中文字符
但没加入UTF-8处理
报错有明显的 non-ascii 提示
脚本的开头加入
# -*- coding:utf-8 -*-
(3)使用了全角的字符
比如全角的引号等
(4) unexpect indent----block缩进的报错,缩进要求很严格!
不规范缩进,block会报错!
像下文,实际上
message='It was a bright cold day in April,and the clocks were striking thirtenn.'
count={}
for character in message:
count.setdefault(character,0) #有时前面的TAB用的不对,重新删了再TAB一次就好了
count[character]=count[character]+1 #有时前面的TAB用的不对,重新删了再TAB一次就好了
print(count)
下面的报错有时候看不懂
unexpect indent 或者是一些 block等
要注意,要么全部空格缩进,要么全部是TAB缩进,不能混用
(5) SyntaxError: invalid syntax
错误原因:
英文是“非法语句”的意思。漏标点符号(例如漏分号,漏&号),多标点符号,拼写错,等等都会造成这种错
解决办法:
检查哪里多了个逗号,冒号等等,关键词拼写语法错误
(6) 缩进插错,打开显示了查更快
比如使用notepad++时,注意使用 视图--显示符号--显示换行符号和空格等
可以查错,比如
(6) list 字典,等其他数组类,表类越界了thon-IndexError: list index out of range
(7)TypeError: 'tuple' object does not support item assignment
元组等不支持修改,只能读 还可以绕?
(8) TypeError: 'int' object is not subscriptab,不可下标访问的类型 ,
TypeError: 'set' object is not subscriptable
(9) iterable
(10) 不可调用的,TypeError: 'list' object is not callable
>>> listB1=[{"apple","pear"},{"red"},{"blue"}]
>>> listB1(1)
Traceback (most recent call last):
File "<pyshell#56>", line 1, in <module>
listB1(1)
TypeError: 'list' object is not callable
因为() 代表函数, 所以一个对象后面直接接() 会被认为是函数,而又没定义过listB1 这个函数
对象的方法,也是 obj.func() 而不是obj()
(11) 对象变量,没有用set的方法引用
这种一般是返回的是对象变量,却没有用set引用
Debug.Print Range("a1:" & "A" & Range("a65536").End(xlUp).Row).Find(TextBox1.Value).Value
Sub test502()
Dim a As Object 'find返回的应该是 range对象
Set a = ActiveWorkbook.ActiveSheet.UsedRange.Find(what:=2, LookIn:=xlValues)
Debug.Print a.Address
End Sub
(12) 类型不匹配,比如 application.find() 不能在数组,多单元格range内查找,只能在一个 字符串内查找
所以,你要熟悉 不同函数的使用方法,范围等特点
Sub test505()
Debug.Print Application.Find(2, "a12345")
Debug.Print Application.Find(3, Range("b10"))
Debug.Print Application.Find("aaaa", Range("b10").Value) '实测VBE不稳定,有时候也会弹出报错
'查不到的内容也会报类型不匹配
'Debug.Print "Application.Find(9, ""a12345"") " & Application.Find(9, "a12345")
'Debug.Print "Application.Find(""aaa"", ""a12345"") " & Application.Find("aaa", "a12345")
'下面会报错类型不匹配
'Debug.Print "Application.Find(1, Array(1, 2, 3)) " & Application.Find(1, Array(1, 2, 3))
'Debug.Print "Application.Find(2, Range(""B1: B2 "")) " & Application.Find(2, Range("B1:B2"))
End Sub