激光除皱价格,红安张磊,纸上谈兵的反义词
在 if、elif、else、for、while、def语句后面忘记添加 :
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 syntax
python用缩进区分代码块,常见的错误用法:
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
if city in ['new york', 'bei jing', 'tokyo']: print('this is a mega city') --------------------------------------------------------------------------- nameerror traceback (most recent call last) <ipython-input-22-a81fd2e7a0fd> in <module> ----> 1 if city in ['new york', 'bei jing', 'tokyo']: 2 print('this is a mega city') nameerror: name 'city' is not defined
if 5>3: print('5比3大') file "<ipython-input-46-47f8b985b82d>", line 1 if 5>3: ^ syntaxerror: invalid character in identifier if 5>3: print('5比3大') file "<ipython-input-47-4b1df4694a8d>", line 2 print('5比3大') ^ syntaxerror: invalid character in identifier spam = [1, 2,3] file "<ipython-input-45-47a5de07f212>", line 1 spam = [1, 2,3] ^ syntaxerror: invalid character in identifier if 5>3: print('5比3大‘) file "<ipython-input-48-ae599f12badb>", line 2 print('5比3大‘) ^ syntaxerror: eol while scanning string literal
字符串/列表/元组 支持拼接
字典/集合不支持拼接
'i have ' + 12 + ' eggs.' #'i have {} eggs.'.format(12) --------------------------------------------------------------------------- typeerror traceback (most recent call last) <ipython-input-29-20c7c89a2ec6> in <module> ----> 1 'i have ' + 12 + ' eggs.' typeerror: can only concatenate str (not "int") to str ['a', 'b', 'c']+'def' --------------------------------------------------------------------------- typeerror traceback (most recent call last) <ipython-input-31-0e8919333d6b> in <module> ----> 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) <ipython-input-33-90742621216d> in <module> ----> 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) <ipython-input-35-ddf5fb1e6c8c> in <module> ----> 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) <ipython-input-36-1b1456844331> in <module> 2 grades2 = {'david':88, 'unique':89} 3 ----> 4 grades1+grades2 typeerror: unsupported operand type(s) for +: 'dict' and 'dict'
spam = ['cat', 'dog', 'mouse'] print(spam[5]) --------------------------------------------------------------------------- indexerror traceback (most recent call last) <ipython-input-38-e0a79346266d> in <module> 1 spam = ['cat', 'dog', 'mouse'] ----> 2 print(spam[5]) indexerror: list index out of range
在字典对象中访问 key 可以使用 []
,
但是如果该 key 不存在,就会导致:keyerror: 'zebra'
spam = {'cat': 'zophie', 'dog': 'basil', 'mouse': 'whiskers'} print(spam['zebra']) --------------------------------------------------------------------------- keyerror traceback (most recent call last) <ipython-input-39-92c9b44ff034> in <module> 3 'mouse': 'whiskers'} 4 ----> 5 print(spam['zebra']) keyerror: 'zebra'
为了避免这种情况,可以使用 get 方法
spam = {'cat': 'zophie', 'dog': 'basil', 'mouse': 'whiskers'} print(spam.get('zebra')) none
key 不存在时,get 默认返回 none
当函数中传入的是函数或者方法时,容易漏写括号
spam = {'cat': 'zophie', 'dog': 'basil', 'mouse': 'whiskers'} print(spam.get('zebra') file "<ipython-input-43-100a51a7b630>", line 5 print(spam.get('zebra') ^ syntaxerror: unexpected eof while parsing
def diyadd(x, y, z): return x+y+z diyadd(1, 2) --------------------------------------------------------------------------- typeerror traceback (most recent call last) <ipython-input-44-7184f3f906ca> in <module> 2 return x+y+z 3 ----> 4 diyadd(1, 2) typeerror: diyadd() missing 1 required positional argument: 'z'
电脑中没有相关的库
如try、except、def、class、object、none、true、false等
try = 5 print(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
import pandas as pd df = pd.read_csv('data/twitter情感分析数据集.csv') df.head()
尝试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': ''
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
python求numpy中array按列非零元素的平均值案例
网友评论