Python开发笔记

不定期更新中......


1. TypeError: unhashable type: 'list'
   字典的key不能为list类型,因为list类型是不可hash的
   字典的key只能为字符串,数值或者元组
   
2. a = b = c = []
   b.append(100)
   
   上面代码运行完后a,b,c都是[100]
   
3. sre_constants.error: look-behind requires fixed-width pattern
   正则匹配中使用后顾匹配时需要定长,也就是后顾正则中不能出现量词
   
4. not writable
   写文件时文件处于关闭状态或者以只读模式打开了文件
   
5. 遍历集合的过程中不要修改集合,copy一份出来

6. sql语句的参数应该在调用execute方法时传入,可以避免注入攻击,以及插入特殊字符时带来的问题
在往execute方法传入参数时,注意将参数以tuple或者list形式传入,例如传入100,则应该使用(100,)或者[100]传参

7.str与unicode的转换
处理字符串的一些函数如lower(),返回的的字符串编码可能会被改变(未深入研究),导致相互转换时报错:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 1: ordinal not in range(128)

例如:message = unicode(message.lower())
如果message在调用lower()函数前是str,则在unicode()函数会报上面的错
改为:message = unicode(message)
      message = message.lower()
正常

8.module 'sys' has no attribute 'setdefaultencoding'
    1.python2中解决方法:
    reload(sys)
    sys.setdefaultencoding('utf-8')

    2.python3中解决方法:
    imp.reload(sys)
    sys.setdefaultencoding('utf-8')

你可能感兴趣的:(Python)