随笔分类 -  Python

摘要:DoesNotExist at /account/User has no account.Request Method:GETRequest URL:http://127.0.0.1:8000/account/Django Version:1.6.1Exception Type:DoesNotExistException Value:User has no account.Exception Location:/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py in __get__, line 20 阅读全文
posted @ 2014-01-02 00:01 LisPythoniC 阅读(374) 评论(0) 推荐(0)
摘要:DoesNotExist at /admin/User has no account.Request Method:GETRequest URL:http://127.0.0.1:8000/admin/Django Version:1.6.1Exception Type:DoesNotExistException Value:User has no account.Exception Location:/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py in __get__, line 206Pyt 阅读全文
posted @ 2014-01-01 23:59 LisPythoniC 阅读(434) 评论(0) 推荐(0)
摘要:"""Django settings for sitea project.For more information on this file, seehttps://docs.djangoproject.com/en/1.6/topics/settings/For the full list of settings and their values, seehttps://docs.djangoproject.com/en/1.6/ref/settings/"""# Build paths inside the project lik 阅读全文
posted @ 2014-01-01 23:57 LisPythoniC 阅读(238) 评论(0) 推荐(0)
摘要:直接:for i in range(1,n+1): print(i) if xls.getCell(i,1,1)=='id': res=[] xls.xlBook.Worksheets(i).Name=xls.xlBook.Worksheets(i).Name.capitalize()才能成功更改每个工作表的名字.这样就不行:for i in range(1,n+1): print(i) if xls.getCell(i,1,1)=='id': res=[] t=xls.xlBook.Worksheets(i).N... 阅读全文
posted @ 2013-12-28 22:06 LisPythoniC 阅读(206) 评论(0) 推荐(0)
摘要:def f(x): print 'original' if x > 0: return f(x-1) return 0g = fdef f(x): print 'new' return xprint g(5)结果是:originalnew4证明了:1.when g(5) runs, origninal `f`is called,not new `f`.2.as 5>0,'return f(x-1)'is executed3.new`f`is called when `f(x-1)` runs. so the result is 4. 阅读全文
posted @ 2013-12-27 11:23 LisPythoniC 阅读(162) 评论(0) 推荐(0)
摘要:真是十分神奇..import win32com.clientimport times = win32com.client.Dispatch("SAPI.SpVoice")s.Speak('哈哈,真有趣,能说中文,还能说English,要不要试试那个什么用一百种语言说我爱你...')我瞬间有种不怕Python做不到就怕自己想不到的感觉. 阅读全文
posted @ 2013-12-19 00:31 LisPythoniC 阅读(294) 评论(0) 推荐(0)
摘要:(?aiLmsux)(One or more letters from the set 'a', 'i', 'L', 'm', 's', 'u', 'x'.) The group matches the empty string; the letters set the corresponding flags: re.A (ASCII-only matching), re.I (ignore case), re.L (locale dependent), re.M (multi-li 阅读全文
posted @ 2013-12-10 12:33 LisPythoniC 阅读(905) 评论(0) 推荐(0)
摘要:两个不起眼但是比较重要的设定Python str类型的字面量解释器当反斜杠及其紧接字符无法构成一个具有特殊含义的序列('recognized escape sequences')时,Python选择保留全部字符.直接看例子:>>> '\c''\\c'>>> '\d''\\d'官方管'\c'这种序列叫'unrecognized escape sequences'.官方文档相应部分:Unlike Standard C, all unrecognized 阅读全文
posted @ 2013-11-28 01:14 LisPythoniC 阅读(6776) 评论(0) 推荐(0)
摘要:在模拟登录上,requests确实比python标准库中的相关模块更加简洁.假设你需要去爬一组页面(targetUrls),而这些页面要登录才能进行访问.那么requests能够提供一种相当简单的语法来实现.不过在此之前,你得先通过浏览器的开发人员工具确定:1.递交用户名和密码的页面(loginUrl)2.键值对(递交数据是以字典的形式)模拟举例:#确定登录页面地址和键值对loginUrl = "http://..."loginData={ 'formhash' : "f474a8c6", 'cookietime' : 2 阅读全文
posted @ 2013-11-25 00:52 LisPythoniC 阅读(5962) 评论(0) 推荐(0)
摘要:摘自:PyWin32.chmIntroductionThis documents how to quickly start using COM from Python. It is not a thorough discussion of the COM system, or of the concepts introduced by COM.Other good information on COM can be found in various conference tutorials - please see the collection of Mark's conference 阅读全文
posted @ 2013-11-20 14:14 LisPythoniC 阅读(492) 评论(0) 推荐(0)
摘要:一言蔽之,搭配是关键.以32位Window为例,先后安装:1.PyQtPyQt4-4.10.3-gpl-Py3.3-Qt4.8.5-x32.exehttp://www.riverbankcomputing.com/software/pyqt/download这个简单,下载后直接双击安装.如果你选择PyQt4-4.10.3-gpl-Py3.3-Qt5.1.0-x32.exe,那么你将失败.附开发人员的解释:Please note that eric5 use with PyQt4/Qt5 is still experimental and does notsupport PyQt5/Qt5 at 阅读全文
posted @ 2013-11-20 12:48 LisPythoniC 阅读(587) 评论(0) 推荐(0)
摘要:对于一些貌似很简单常见的函数,最好还是去读一下Python文档,否则当你被某个BUG折磨得死去活来时,还不知根源所在.尤其是Python这种不断更新的语言.(python 2.7 的round和3.3.2不一样)3.3.2官方文档对round的定义round(number[,ndigits])Return the floating point valuenumberrounded tondigitsdigits after the decimal point. Ifndigitsis omitted, it defaults to zero. Delegates tonumber.__roun 阅读全文
posted @ 2013-11-19 01:04 LisPythoniC 阅读(10774) 评论(1) 推荐(2)
摘要:idict是dict的子类,它的键值和属性是同步的,并且有强大的默认值机制.例如,假设x是idict的一个实例,且x['a']['b']=12,则有x.a.b=12.反之亦然;假设'c'不在x的键集合,那么尝试访问x['c']或者x.c,均会直接初始化默认值.class idict(dict): "super dict class, attributes and keys are equal" def __init__(self,d={},dft=None): idict.dft=dft for k,v in 阅读全文
posted @ 2013-11-17 23:27 LisPythoniC 阅读(1324) 评论(0) 推荐(0)
摘要:定义标准类dict的一个子类c:>>> class c(dict): pass>>> y=c({1:2,3:4})>>> y{1: 2, 3: 4}>>> y.__dict__{}>>> z={1:2,3:4}>>> z.__dict__Traceback (most recent call last): File "", line 1, in z.__dict__AttributeError: 'dict' object has no attribu 阅读全文
posted @ 2013-11-17 22:19 LisPythoniC 阅读(457) 评论(0) 推荐(0)
摘要:我之前只知道设置初始值0.今天偶然在Python算法书上看到这个片段,从100个随机数里面找2个最靠近的自然数(不相等):from random import randrange seq = [randrange(10**10) for i in range(100)] dd = float("inf") for x in seq: for y in seq: if x == y: continue d = abs(x-y) if d >> float('-Inf')==-float('Inf')True顺便还收获了NaN的概念, 阅读全文
posted @ 2013-11-12 11:05 LisPythoniC 阅读(40739) 评论(0) 推荐(0)
摘要:这应该是我编程以来完成的难度最大的一个函数了.因为可能存在的情况非常多,需要设计合理的参数来控制解析流程.经验概要:1.大胆假设一些子功能能够实现,看能否建立整个框架.如果在假设的基础上都无法建立,那么必定需要更换思路.2.穷举所有可能的情况,想清楚该怎么应对它们.如果写完了整个程序才发现,啊!漏了一种情形.代价或许就是全部推翻重写.我算是能够理解为什么教科书上推荐设计程序之前要先画流程图,那实际是要求你编程之前要先知道解法,并以一种直观的形式保存.这样,你在将解法翻译为代码时,思路才清晰,效率才高.什么?难道有人会在不知道解法的情况下就编程?显然有这种人的,我这次就是这样干的.我这次是吃够了 阅读全文
posted @ 2013-11-11 01:03 LisPythoniC 阅读(1045) 评论(0) 推荐(0)
摘要:python 为何要设计这种?正确:def f(a=2,b=2,c=3): return a+b+cprint(f(*(1,1),c=1))错误:def f(a=2,b=2,c=3): return a+b+cprint(f(*(1,1),1))会被提示:而这个会被提示:def f(a=2,b=2,c=3): return a+b+cprint(f(a=1,*(1,1)))>>> Traceback (most recent call last): File "C:\Users\un_ieval.py", line 91, in print(f(a=1,* 阅读全文
posted @ 2013-11-10 20:34 LisPythoniC 阅读(244) 评论(0) 推荐(0)
摘要:待处理:>>> re.findall(r'[-+]*\d+(?:\.\d+)?','-++++---+123.012')['-++++---+123.012']>>> re.findall(r'[-+]*\d+(\.\d+)?','-++++---+123.012')['.012'] 阅读全文
posted @ 2013-11-08 18:02 LisPythoniC 阅读(170) 评论(0) 推荐(0)
摘要:前几天看一个网友提问,如何计算'1+1'这种字符串的值,不能用eval函数.我仿佛记得以前新手时,对这个问题完全不知道如何下手.我觉得处理括号实在是太复杂了,多层嵌套括号怎么解析呢?一些多余的括号呢?而在没有括号的情况下,处理不同运算符之间的优先级又很头疼.而'**'这种占2个字符的运算符,还有着奇特的优先级规则,x**y**z应先计算y**z,并且如果z后面还跟着**的话,则不能先计算y**z..这是最难的地方!但是,今天我竟然把它实现了!而且是用最原始的函数实现的,它们是:str,s.isdigit,s.find,s.count,dict,floatif,i 阅读全文
posted @ 2013-11-08 00:40 LisPythoniC 阅读(1003) 评论(0) 推荐(0)
摘要:下面这个错误是由于在正则[...]的内部,减号'-'是一个有特殊含义的字符(代表字符范围)所以如果需要在[...]内匹配减号'-',需要用反斜杠'\'转义.>>> import re>>> re.split(r'[+\-*/%]','+a-b-c')['', 'a', 'b', 'c']>>> re.split(r'[+-*/%]','+a-b-c')Tracebac 阅读全文
posted @ 2013-11-06 22:20 LisPythoniC 阅读(992) 评论(0) 推荐(0)