摘要: windows 默认换行符为 \r\n;unix默认换行符为 \n;所以当win下编辑的脚本在linux下显示末尾多了^M:换行符修改为同一的unix格式脚本如下: 1 def run(path,file): 2 for file in files: 3 file = path+'\\'+file 4 f = open(file,'r') 5 result = f.read() 6 print result 7 result = result.replace(r'\r\n',r'\n') 8 ... 阅读全文
posted @ 2013-09-29 17:53 Sudawei 阅读(3120) 评论(0) 推荐(0) 编辑
摘要: python中 __name__及__main()__的使用1 #hello.py 2 def sayHello():3 str="hello" print(str);4 5 if __name__ == "__main__": 6 print ('This is main of module "hello.py"') 7 sayHello()python作为一种脚本语言,我们用python写的各个module都可以包含以上那么一个累死c中的main函数, 只不过python中的这种__main__与c中有一些区别,主 阅读全文
posted @ 2013-09-29 17:40 Sudawei 阅读(220) 评论(0) 推荐(0) 编辑
摘要: 参考:http://luchanghong.com/python/2012/07/11/python-urlencode-and-urldecode.html概要:python 通过 HTTP 交互处理数据的时候,url 里面的中文以及特殊字符要做处理的,来学习一下 urlencode 与 urldecode 之间相互转换的方法。当url地址含有中文,或者参数有中文的时候,这个算是很正常了,但是把这样的url作为参数传递的时候(最常见的callback),需要把一些中文甚至'/'做一下编码转换。一、urlencodeurllib库里面有个urlencode函数,可以把key-v 阅读全文
posted @ 2013-09-29 17:35 Sudawei 阅读(2958) 评论(0) 推荐(1) 编辑
摘要: Windows下面python报错会用到:WindowsError的错误代码详解0操作成功完成。1功能错误。2系统找不到指定的文件。3系统找不到指定的路径。4系统无法打开文件。5拒绝访问。6句柄无效。7存储控制块被损坏。8存储空间不足,无法处理此命令。9存储控制块地址无效。10环境错误。11试图加载格式错误的程序。12访问码无效。13数据无效。14存储器不足,无法完成此操作。15系统找不到指定的驱动器。16无法删除目录。17系统无法将文件移到不同的驱动器。18没有更多文件。19介质受写入保护。20系统找不到指定的设备。21设备未就绪。22设备不识别此命令。23数据错误(循环冗余检查)。24程序 阅读全文
posted @ 2013-09-29 17:31 Sudawei 阅读(1699) 评论(0) 推荐(0) 编辑
摘要: import randomrandom.randomrandom.random()用于生成一个0到1的随机符点数: 0 b,则生成的随机数n: a <= n <= b。如果 a <b, 则 b <= n <= a。print random.uniform(10, 20)print random.uniform(20, 10)#---- 结果(不同机器上的结果不一样) #18.7356606526 #12.5798298022random.randintrandom.randint()的函数原型为:random.randint(a, b),用于生成一个指定范围内的整 阅读全文
posted @ 2013-09-29 17:26 Sudawei 阅读(232) 评论(0) 推荐(0) 编辑
摘要: 递归删除文件或目录:递归,os.walk删除目录,shutil.rmtree 1 # -*- coding: UTF-8 -*- 2 import os.path,sys,shutil 3 4 path = 'H:/test' 5 6 7 for root ,dirs ,files in os.walk(path): 8 print root,dirs,files 9 for name in dirs:10 if name == 'svn':11 print name12 shutil.rmtree(os.p... 阅读全文
posted @ 2013-09-29 17:03 Sudawei 阅读(4760) 评论(0) 推荐(1) 编辑
摘要: 参考:http://www.cnblogs.com/PythonHome/archive/2011/11/19/2255459.html这个模块提供了与 Perl 相似l的正则表达式匹配操作。Unicode字符串也同样适用。正则表达式使用反斜杠" \ "来代表特殊形式或用作转义字符,这里跟Python的语法冲突,因此,Python用" \\\\ "表示正则表达式中的" \ ",因为正则表达式中如果要匹配" \ ",需要用\来转义,变成" \\ ",而Python语法中又需要对字符串中每一个\进行 阅读全文
posted @ 2013-09-29 16:52 Sudawei 阅读(835) 评论(0) 推荐(0) 编辑
摘要: 参考:http://blog.csdn.net/wklken/article/details/7364390http://hankjin.blog.163.com/blog/static/3373193720105140583594/1、基本用法:1 res=urllib2.urlopen(url)2 print res.read()2. 加上要get或post的数据1 data={"name":"hank", "passwd":"hjz"}2 urllib2.urlopen(url, urllib.urlenco 阅读全文
posted @ 2013-09-29 15:58 Sudawei 阅读(2208) 评论(0) 推荐(0) 编辑
摘要: 问题:postgreSQL insert 字段包含单引号,如:insert into table values('1001','tom'cat'),执行报错;解决:将单引号替换为两个单引号,如 detail = detail.replace("'","''"),变为:insert into table values('1001','tom''cat')参考:http://www.itpub.net/thread-780335-1-1.html 阅读全文
posted @ 2013-09-29 15:42 Sudawei 阅读(3187) 评论(0) 推荐(0) 编辑
摘要: django 常用命令,备忘:django-admin.py startproject test 创建一个项目名叫test的项目python manage.py startapp app 创建一个叫做app的APPpython manage.py runserver 启动服务器python manage.py validate 检查模型的有效性(说通俗点就是这个模型能不能在数据库中生成表,它只是检测,并没有真正生成)python manage.py sqlall app 打印这个app中创建数据... 阅读全文
posted @ 2013-09-29 15:38 Sudawei 阅读(257) 评论(0) 推荐(0) 编辑
摘要: wsgi接口,使用gunicorn作为server,想在外层加nginx。配置了proxy_pass http://127.0.0.1:9008;访问报301。参考gunicorn 官网配置: 1 worker_processes 1; 2 3 user nobody nogroup; 4 pid /tmp/nginx.pid; 5 error_log /tmp/nginx.error.log; 6 7 events { 8 worker_connections 1024; 9 accept_mutex off;10 }11 12 http {13 include ... 阅读全文
posted @ 2013-09-29 15:33 Sudawei 阅读(2502) 评论(0) 推荐(0) 编辑