python构建知识库_Python学习知识库

本文列举并解析了Python编程中的一些常见错误,包括过于宽泛的异常处理、不正确的函数调用方式、字典创建优化、元组不可变性、文件操作中的缩进错误、循环中使用tell()的问题、eval()的使用限制以及导入模块错误。同时,提供了对应的解决办法和代码优化建议,帮助开发者提升代码质量。
摘要由CSDN通过智能技术生成

2017年10月16日

1. too broad exception clause

捕获的异常过于宽泛了,没有针对性,应该指定精确的异常类型

场景:

defcheck_data_type(column, value)try:#根据数据字典中列的类型,对value进行数据类型转换(value为str)

returnTrueexcept:print("列%s的值类型不正确", column)

2. 场景

from core importsearchfrom core importupdatefrom core importcreatefrom core importremovedefrun():

sql_prefix={"select": search,"update": update,"insert": create,"delete": remove

}print("欢迎使用员工信息管理系统,输入SQL语句管理数据,输入exit退出程序\n")

statement= input("SQL>")

sql=statement.strip().lower()while sql != 'exit':if sql[0:6] insql_prefix:

sql_prefix[sql[0:6]](sql)else:print("无效的语句\n")

想通过以上方式调用相应的函数,提示错误: TypeError: 'module' object is not callable

原因:举例说明,当输入查询语句时,调用的函数是search(sql),而实际调用方式应该是serach.search(sql),因此无法调用

解决:在文件内声明相应的函数,调用search.search(sql)

defselect(sql):

search.search(sql)defupdate(sql):

replace.update(sql)definsert(sql):

create.create(sql)defdelete(sql):

remove.remove(sql)

3. python this dictionary creation could be rewritten a dictionary literal

d ={}

d["a"]=1

修改为

d =dict()

d["a"]=1

4. tuples don't support item assignment

元祖不支持元素修改,这是由于修改函数中的元组参数引起的

5. continuatation line under-indented for visual indent

一行中的字符在换行是存在问题

def check_data_type(column, value)

try:

# 根据数据字典中列的类型,对value进行数据类型转换(value为str)

return True

except:

print("列%s的值类型不正确", column)

2017年7月29日

>>> with open("login.txt","r+",ending="utf-8") as f:

...f.readline()

File "", line 2     f.readline()     ^ IndentationError: expected an indented block

原因:没有缩进,要在新行加空格或tab

with open("users_info", "rw", encoding="utf-8") as f:     for line in f:

if "lisi" in line:

f.seek(f.tell()-len(line))

line="test 123 0"

f.write(line)

OSError: telling position disabled by next() call

原因:在循环中不能使用tell(),具体原因需要深入研究

TypeError: eval() arg 1 must be a string, bytes or code object

原因:对一个不是str,bytes,code object的类型使用eval()方法,例如我在终端中设置a = {“zhangsan","123"},eval(a),这是对字典使用了eval()方法,正确的方式是a=' {“zhangsan","123"}',eval(a)

python模块以及导入出现ImportError: No module named ‘xxx’问题

原因:根据我学习java的经验,导入错误是因为将代码放在了direction(package)下面,所以导入要加包前缀

例如:如果在模块m1下建立test.py文件,导入时应该为import m1.test

Traceback (most recent call last):

File "C:/Users/xintongwangluosuo/PycharmProjects/tasks/module1/three_level_menu/three_leval_menu.py", line 20, in

current_layer = layer[-1]

KeyError: -1

layer=current_layer

current_layer=current_layer[choice]if notcurrent_layer:print("没有下一级菜单")

current_layer= layer[-1]

layer.pop()

上面是编写一个“三级菜单”程序时的代码片段,出错提示KeyError: -1,是说-1这个key在layer中是不存在的

layer本意是定义为一个列表,保存当前菜单层级的字典值,直接赋值将layer类型改成了字典

因此代码应该修改成layer.append(current_layer)

PEP8代码风格错误

expected 2 blank lines, found 0 上面需要两行空白,找到0行

原因:文件代码应在注释后面空两行

Typo:In word 错误拼写

原因:单词拼写错误

变量、值、运算符前后应加空格,逗号后应加空格,输出字符串时应采用诸如"test%s", % name的方式,%前后要有空格

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值