在 if、elif、异常遇else、初学常见错误处for、异常遇while、初学常见错误处def语句后面忘记添加 :age = 42if age == 42 print(Hello!)
age = 42 if age == 42 print ( Hello! ) File "<ipython-input-19-4303141d6f97>" ,异常遇 line 2 if age == 42 ^ SyntaxError : invalid syntax= 是赋值操作,而判断两个值是初学常见错误处否相等是 ==
gender = 男 if gender = 男 : print ( Man ) File "<ipython-input-20-191d01f95984>" , line 2 if gender = 男 : ^ SyntaxError : invalid syntaxPython用缩进区分代码块,常见的异常遇错误用法:
print(Hello!) print(Howdy!) File "<ipython-input-9-784bdb6e1df5>", line 2 print(Howdy!) ^ IndentationError: unexpected indent num = 25 if num == 25: print(Hello!) File "<ipython-input-21-8e4debcdf119>", line 3 print(Hello!) ^ IndentationError: expected an indented block字符串/列表/元组 支持拼接
字典/集合不支持拼接
I have + 12 + eggs. #I have { } eggs..format(12) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) in ----> 1 I have + 12 + eggs. TypeError: can only concatenate str (not "int") to str [a, b, c]+def --------------------------------------------------------------------------- TypeError Traceback (most recent call last) in ----> 1 [a, b, c]+def TypeError: can only concatenate list (not "str") to list (a, b, c)+[a, b, c] --------------------------------------------------------------------------- TypeError Traceback (most recent call last) in ----> 1 (a, b, c)+[a, b, c] TypeError: can only concatenate tuple (not "list") to tuple set([a, b, c])+set([d, e]) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) in ----> 1 set([a, b, c])+set([d, e]) TypeError: unsupported operand type(s) for +: set and set grades1 = { Mary:99, Henry:77} grades2 = { David:88, Unique:89} grades1+grades2 --------------------------------------------------------------------------- TypeError Traceback (most recent call last) in <module> 2 grades2 = { David:88, Unique:89} 3 ----> 4 grades1+grades2 TypeError: unsupported operand type(s) for +: dict and dict在字典对象中访问 key 可以使用 [],
但是初学常见错误处如果该 key 不存在,就会导致:KeyError: zebra
spam = { cat: Zophie,异常遇 dog: Basil, mouse: Whiskers} print(spam[zebra]) --------------------------------------------------------------------------- KeyError Traceback (most recent call last) in 3 mouse: Whiskers} 4 ----> 5 print(spam[zebra]) KeyError: zebra为了避免这种情况,可以使用 get 方法
spam = { cat: Zophie,初学常见错误处 dog: Basil, mouse: Whiskers} print(spam.get(zebra)) Nonekey 不存在时,get 默认返回 None
当函数中传入的异常遇是函数或者方法时,容易漏写括号
spam = { cat: Zophie,初学常见错误处 dog: Basil, mouse: Whiskers} print(spam.get(zebra) File "", line 5 print(spam.get(zebra) ^ SyntaxError: unexpected EOF while parsing电脑中没有相关的库
如try、云服务器except、def、class、object、None、True、False等
try = 5print(try) File " <ipython-input-1-508e87fe2ff3>", line 1 try = 5 ^ SyntaxError: invalid syntax def = 6 print(6) File "<ipython-input-2-d04205303265>", line 1 def = 6 ^ SyntaxError: invalid syntax尝试encoding编码参数传入utf-8、gbk
df = pd.read_csv(data/twitter情感分析数据集.csv, encoding=utf-8) df.head()都报错说明编码不是utf-8和gbk,而是不常见都编码,这里我们需要传入正确都encoding,才能让程序运行。
python有个chardet库,专门用来侦测编码。香港云服务器
import chardet binary_data = open(data/twitter情感分析数据集.csv, rb).read() chardet.detect(binary_data) { encoding: Windows-1252, confidence: 0.7291192008535122, language: }