十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
在 if、elif、else、for、while、def語句后面忘記添加 :

age = 42
if age == 42
print('Hello!')
File "", line 2
if age == 42
^
SyntaxError: invalid syntax ='''
遇到問題沒人解答?小編創(chuàng)建了一個Python學(xué)習(xí)交流QQ群:××× 尋找有志同道合的小伙伴,
互幫互助,群里還有不錯的視頻學(xué)習(xí)教程和PDF電子書!
'''
=` 是賦值操作,而判斷兩個值是否相等是 `==
gender = '男'
if gender = '男':
print('Man')
File "", line 2
if gender = '男':
^
SyntaxError: invalid syntax Python用縮進區(qū)分代碼塊,常見的錯誤用法:
print('Hello!')
print('Howdy!')
File "", line 2
print('Howdy!')
^
IndentationError: unexpected indent
num = 25
if num == 25:
print('Hello!')
File "", line 3
print('Hello!')
^
IndentationError: expected an indented block '''
遇到問題沒人解答?小編創(chuàng)建了一個Python學(xué)習(xí)交流QQ群:××× 尋找有志同道合的小伙伴,
互幫互助,群里還有不錯的視頻學(xué)習(xí)教程和PDF電子書!
'''
if city in ['New York', 'Bei Jing', 'Tokyo']:
print('This is a mega city')
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
in
----> 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 "", line 1
if 5>3:
^
SyntaxError: invalid character in identifier
if 5>3:
print('5比3大')
File "", line 2
print('5比3大')
^
SyntaxError: invalid character in identifier
spam = [1, 2,3]
File "", line 1
spam = [1, 2,3]
^
SyntaxError: invalid character in identifier
if 5>3:
print('5比3大‘)
File "", line 2
print('5比3大‘)
^
SyntaxError: EOL while scanning string literal 字符串/列表/元組 支持拼接
字典/集合不支持拼接
'''
遇到問題沒人解答?小編創(chuàng)建了一個Python學(xué)習(xí)交流QQ群:××× 尋找有志同道合的小伙伴,
互幫互助,群里還有不錯的視頻學(xué)習(xí)教程和PDF電子書!
'''
'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
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)
in
1 spam = ['cat', 'dog', 'mouse']
----> 2 print(spam[5])
IndexError: list index out of range 在字典對象中訪問 key 可以使用 [],
但是如果該 key 不存在,就會導(dǎo)致:KeyError: 'zebra'
'''
遇到問題沒人解答?小編創(chuàng)建了一個Python學(xué)習(xí)交流QQ群:××× 尋找有志同道合的小伙伴,
互幫互助,群里還有不錯的視頻學(xué)習(xí)教程和PDF電子書!
'''
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
當函數(shù)中傳入的是函數(shù)或者方法時,容易漏寫括號
'''
遇到問題沒人解答?小編創(chuàng)建了一個Python學(xué)習(xí)交流QQ群:××× 尋找有志同道合的小伙伴,
互幫互助,群里還有不錯的視頻學(xué)習(xí)教程和PDF電子書!
'''
spam = {'cat': 'Zophie',
'dog': 'Basil',
'mouse': 'Whiskers'}
print(spam.get('zebra')
File "", 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)
in
2 return x+y+z
3
----> 4 diyadd(1, 2)
TypeError: diyadd() missing 1 required positional argument: 'z' 電腦中沒有相關(guān)的庫
如try、except、def、class、object、None、True、False等
'''
遇到問題沒人解答?小編創(chuàng)建了一個Python學(xué)習(xí)交流QQ群:××× 尋找有志同道合的小伙伴,
互幫互助,群里還有不錯的視頻學(xué)習(xí)教程和PDF電子書!
'''
try = 5
print(try)
File "", line 1
try = 5
^
SyntaxError: invalid syntax
def = 6
print(6)
File "", line 1
def = 6
^
SyntaxError: invalid syntax import pandas as pd
df = pd.read_csv('data/twitter情感分析數(shù)據(jù)集.csv')
df.head()嘗試encoding編碼參數(shù)傳入utf-8、gbk
df = pd.read_csv('data/twitter情感分析數(shù)據(jù)集.csv', encoding='utf-8')
df.head()都報錯說明編碼不是utf-8和gbk,而是不常見都編碼,這里我們需要傳入正確都encoding,才能讓程序運行。
python有個chardet庫,專門用來偵測編碼。
import chardet
binary_data = open('data/twitter情感分析數(shù)據(jù)集.csv', 'rb').read()
chardet.detect(binary_data)
{'encoding': 'Windows-1252', 'confidence': 0.7291192008535122, 'language': ''另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。