1、代码编辑时的常见错误:
- 1、unindent does not match any outer indentation level
- 有的语句的缩进不是用tab,而是直接用了几个空格代替编译时出了问题
- 2、IndentationError:expected an indented block错误解决
- 有空格或缩进错误
- 3、syntaxError:Non-ASCII character ‘\xe8’ in file…:
- 在文件的开头添加下面的两行
#!/usr/bin/env python
# coding=utf-8
2、常见的语法上的错误:
- 1、for模块、函数定义模块等要加‘:’
- 2、python3.x的版本的python要用print(a) ,即加括号,不用print a
- 3、对象不能作为字典的键值key
3、IO上常见的错误:
- 1、读入外部数据需要读入字节数据时,出现TypeError: must be str, not bytes
- open(filename, ‘w’)改为open(filename, ‘wb’)
- 同理,fr = open(filename, ‘rb’)
- 2、使用python自带的open()函数打开txt文件,出现valueError:embedded null character
- 不要以纯数字作为txt文件的开头
4、其他
- 1、TypeError: ‘dict_keys’ object does not support indexing
- python2中,dict.key()返回的是list;而python3中返回的是dict_key对象,故需要变成如:list(myTree.keys())[0]