十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
在 if、elif、else、for、while、def语句后面忘记添加 :
age = 42
if age == 42
print('Hello!')
File "", line 2
if age == 42
^
SyntaxError: invalid syntax
=
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:××× 寻找有志同道合的小伙伴,
互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
=` 是赋值操作,而判断两个值是否相等是 `==
gender = '男'
if gender = '男':
print('Man')
File "", line 2
if gender = '男':
^
SyntaxError: invalid syntax
Python用缩进区分代码块,常见的错误用法:
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
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:××× 寻找有志同道合的小伙伴,
互帮互助,群里还有不错的视频学习教程和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
字符串/列表/元组 支持拼接
字典/集合不支持拼接
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:××× 寻找有志同道合的小伙伴,
互帮互助,群里还有不错的视频学习教程和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 不存在,就会导致:KeyError: 'zebra'
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:××× 寻找有志同道合的小伙伴,
互帮互助,群里还有不错的视频学习教程和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'))
None
key 不存在时,get 默认返回 None
当函数中传入的是函数或者方法时,容易漏写括号
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:××× 寻找有志同道合的小伙伴,
互帮互助,群里还有不错的视频学习教程和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'
电脑中没有相关的库
如try、except、def、class、object、None、True、False等
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:××× 寻找有志同道合的小伙伴,
互帮互助,群里还有不错的视频学习教程和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情感分析数据集.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': ''
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。