pycharm报错集锦

1:SyntaxError: positional argument follows keyword argument

转自:https://blog.csdn.net/weixin_42168614/article/details/88429838

出现这个bug的原因在于参数位置不正确,关键字参数必须跟随在位置参数后面! 因为python函数在解析参数时, 是按照顺序来的, 位置参数是必须先满足, 才能考虑其他可变参数.

例如:

  1.  for line in open('../prepare/train_data/train_data0.txt',encoding='utf-8','r').readlines():
  2.  corpus.append(line)

这是错误的,正确的应该是

  1.  for line in open('../prepare/train_data/train_data0.txt','r',encoding='utf-8').readlines():
  2. corpus.append(line)

2:IndexError: list index out of range

python中出现list index out of range有两种情况:

第1种可能情况:list[index]index超出范围bai,也就是常说的数组越界。
第2种可能情况:list是一个空的, 没有一个元素,进行list[0]就会出现该错误,这在爬虫问题中很常见,比如有个列表爬去下来为空,统一处理就会报错。

 
3:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb5 in position 0: invalid start byte
  1.  
    编码问题:f = open(txtPath,'r',encoding='utf-8')
  2.  
    改为:f = open(txtPath,'r',encoding='gbk')即可
原文地址:https://www.cnblogs.com/hsyfighting/p/14388540.html