Python学习知识库

2017年10月16日

1. too broad exception clause

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

def check_data_type(column, value)
    try:
        # 根据数据字典中列的类型,对value进行数据类型转换(value为str)
        return True
    except:
        print("列%s的值类型不正确", column)


2. 场景

from core import search
from core import update
from core import create
from core import remove

def run():
    sql_prefix = {
        "select": search,
        "update": update,
        "insert": create,
        "delete": remove
    }
    print("欢迎使用员工信息管理系统,输入SQL语句管理数据,输入exit退出程序
")
    statement = input("SQL> ")
    sql = statement.strip().lower()
    while sql != 'exit':
        if sql[0:6] in sql_prefix:
            sql_prefix[sql[0:6]](sql)
        else:
            print("无效的语句
")

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

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

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

def select(sql):
    search.search(sql)


def update(sql):
    replace.update(sql)


def insert(sql):
    create.create(sql)


def delete(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 "<stdin>", 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 <module>
    current_layer = layer[-1]
KeyError: -1

        layer=current_layer
        current_layer = current_layer[choice]
        if not current_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的方式,%前后要有空格

原文地址:https://www.cnblogs.com/koctr/p/7257561.html