python
文章平均质量分 61
xiaoxinyu316
每天进步一点点。
展开
-
python re.search 和 re.match 正则表达式
原文:http://www.111cn.net/phper/157/37171_1.htm一 re.search 和 re.matchpython提供了2中主要的正则表达式操作:re.match 和 re.search。match :只从字符串的开始与正则表达式匹配,匹配成功返回matchobject,否则返回none;search :将字符串的所有字串尝试与正则表达式转载 2015-09-29 10:49:47 · 7876 阅读 · 0 评论 -
Python处理JSON
http://liuzhijun.iteye.com/blog/1859857概念序列化(Serialization):将对象的状态信息转换为可以存储或可以通过网络传输的过程,传输的格式可以是JSON、XML等。反序列化就是从存储区域(JSON,XML)读取反序列化对象的状态,重新创建该对象。JSON(JavaScript Object Notation):一种轻量级数转载 2016-04-09 22:55:54 · 1173 阅读 · 0 评论 -
http://blog.sina.com.cn/s/blog_4452275001012vzl.html
#!/usr/bin/env python #在文件头部 ( 第一行 ) 加上 设置 Python 解释器 # -*- coding: utf-8 -*- #在文件头部 ( 第二行 ) 加上 在编辑器中设置以 UTF-8 默认编码保存文件 # Copyright (c) 2011 - chinakr #版权信息放在文件头部,文件编码之后转载 2016-04-16 22:47:36 · 4579 阅读 · 0 评论 -
编译Python源代码
标题无“转载”即原创文章,版权所有。转载请注明来源:http://hgoldfish.com/blogs/article/63/。在日常生活中,Python代码一般是不编译的,几个py文件复制来就能用。再加上脚本语言的名头,有些不太了解Python的朋友就以为Python没有编译这个过程。其实,虽然Python是脚本语言,但它与Java和C#一样,只能执行字节码。只是Python将编译过程隐藏转载 2016-06-29 18:15:48 · 4880 阅读 · 0 评论 -
perl常用命令
perl -MCPAN -e shellcpan> install HTML::Template转载 2015-06-05 10:21:08 · 1772 阅读 · 0 评论 -
python测试心得
assertEquals(first,second[,msg])assertTrue(expression[,msg])assertFalse(expression[,msg])assertRaises(exception,callable,...)使用规则:assertEquals is prefered over assertTrue and assertFalse原创 2016-08-29 22:19:37 · 539 阅读 · 0 评论 -
Python模块学习 ---- httplib HTTP协议客户端实现
httplib 是 python中http 协议的客户端实现,可以使用该模块来与 HTTP 服务器进行交互。httplib的内容不是很多,也比较简单。以下是一个非常简单的例子,使用httplib获取google首页的html:[python] view plaincopy#coding=gbk import httplib conn转载 2015-06-24 10:55:35 · 2012 阅读 · 0 评论 -
python学习错误总结
ImportError: No module named argparse Solve:easy_install argparse原创 2015-06-19 22:16:30 · 4793 阅读 · 0 评论 -
python利用unittest进行测试用例执行的几种方式
原文:http://www.51testing.com/html/10/448910-3648852.html利用python进行测试时,测试用例的加载方式有2种: 一种是通过unittest.main()来启动所需测试的测试模块; 一种是添加到testsuite集合中再加载所有的被测试对象,而testsuit里存放的就是所需测试的用例,下面分别列出3种方法的具体使用方式:1、转载 2016-11-15 13:43:38 · 21692 阅读 · 0 评论 -
谈谈 Python 程序的运行原理
原文 http://www.cnblogs.com/restran/p/4903056.html这篇文章准确说是『Python 源码剖析』的读书笔记,整理完之后才发现很长,那就将就看吧。1. 简单的例子先从一个简单的例子说起,包含了两个文件 foo.py 和 demo.py[foo.py]def add(a, b): return a + b[dem转载 2016-04-07 22:42:20 · 471 阅读 · 0 评论 -
python清空字典的两种方法比较
http://www.pythontab.com/html/2013/pythonjichu_0507/385.html这里说的clear是指清空python中的字典内容,与删除该字典不一样。例如:d={'name':1,'age':2}想要清空该字典有两个方法方法1:d.clear()打印出d 的值为{}方法2:d={}转载 2016-04-07 22:21:20 · 21849 阅读 · 0 评论 -
Python Requests快速入门
快速上手迫不及待了吗?本页内容为如何入门Requests提供了很好的指引。其假设你已经安装了Requests。如果还没有, 去 安装 一节看看吧。首先,确认一下:Requests 已安装Requests是 最新的让我们从一些简单的示例开始吧。发送请求使用Requests发送网络请求非常简单。一开始要导入Requests模块:>转载 2016-04-07 22:00:39 · 366 阅读 · 0 评论 -
python中对list去重的多种方法
这篇文章主要介绍了python中对list去重的多种方法,本文去重的前提是要保证顺序不变,本文给出了多种实现方法,需要的朋友可以参考下今天遇到一个问题,在同事随意的提示下,用了 itertools.groupby 这个函数。不过这个东西最终还是没用上。问题就是对一个list中的新闻id进行去重,去重之后要保证顺序不变。直观方法最简单的思路就是:复转载 2015-11-05 23:21:38 · 531 阅读 · 0 评论 -
pycharm使用总结
You can remove spaces with SHIFT TAB.You can also use CTRL+ALT+I to auto-indent the selection原创 2015-12-02 13:42:44 · 398 阅读 · 0 评论 -
pip安装使用详解
http://www.ttlsa.com/python/how-to-install-and-use-pip-ttlsa/pip类似RedHat里面的yum,安装Python包非常方便。本节详细介绍pip的安装、以及使用方法。1、pip下载安装1.1 pip下载1# wget "https://pypi.python.org/packages/source/转载 2015-12-03 22:39:44 · 630 阅读 · 0 评论 -
https(ssl)连接之python实现
今天写代码时碰到一个问题,花了几个小时的时间google,基本上把google搜索的前几页内容都一一看了下,问题最终是解决了,不过过程挺曲折的,所以把这个过程记下来以便以后参考之。原因是以下一段代码引起的:?12import urllib2urllib2.urlopen('https://xxxx.com')转载 2015-12-03 23:38:33 · 36124 阅读 · 0 评论 -
如何找到的路径SSL证书文件?
我想Python的请求得到网页中的内容(比如,我可以看到在浏览器页面,并且我可以“查看证书”。 所以现在我想获得与请求的网页,所以我做的:import requestsrequests.get(' CodeGo.net 但后来我得到一个SSLError:SSLError: [Errno 1] _ssl.c:504: error:14090086:SSL routines:SSL转载 2015-12-03 23:34:54 · 12352 阅读 · 0 评论 -
python读取xml文件
原文:http://www.cnblogs.com/fnng/p/3581433.html关于python读取xml文章很多,但大多文章都是贴一个xml文件,然后再贴个处理文件的代码。这样并不利于初学者的学习,希望这篇文章可以更通俗易懂的教如何使用python 来读取xml 文件。 什么是xml?xml即可扩展标记语言,它可以用来标记数据、定义数据类型,是一种允许用转载 2016-03-16 23:08:42 · 836 阅读 · 0 评论 -
python 遍历目录下所有文件
#!/usr/bin/python# -*- coding: utf-8 -*-import osdef gci(filepath):#遍历filepath下所有文件,包括子目录 files = os.listdir(filepath) for fi in files: fi_d = os.path.join(filepath,fi) if转载 2016-03-04 23:03:14 · 1849 阅读 · 0 评论 -
好RESTful API的设计原则
原文 http://www.cnblogs.com/moonz-wu/p/4211626.html说在前面,这篇文章是无意中发现的,因为感觉写的很好,所以翻译了一下。由于英文水平有限,难免有出错的地方,请看官理解一下。翻译和校正文章花了我大约2周的业余时间,如有人愿意转载请注明出处,谢谢^_^ Principles of good RESTful API Design好REST转载 2016-03-09 23:59:16 · 848 阅读 · 0 评论 -
对于django的tastypie的理解
http://techblog.insureme.com.tw/2012/03/tastypiedjango-api.html透過 Tastypie ,可將 Django 後端定義的 Model 快速的變成 API ,讓我們可以很方便的透過 GET / POST / PUT ,取得JSON格式的資料,或是更進一步的對後端資料庫操作。restful其实就是一种转载 2016-04-07 17:31:56 · 1655 阅读 · 1 评论 -
经常使用的一个python logging封装,支持同时向console和文件输出
原文:http://www.cnblogs.com/Jerryshome/archive/2012/02/01/2334444.html为了调试方便,特意将python的logging模块封装了一下,支持同时向console和file输出,支持日志文件回滚。(1)myloggingconfig.py# -*- coding: utf-8 -*-'''Cre转载 2016-11-26 19:38:48 · 6545 阅读 · 0 评论 -
Python logging模块详解
一、简单将日志打印到屏幕:[python] view plaincopyimport logging logging.debug('debug message') logging.info('info message') logging.warning('warning message') logging.error('err转载 2016-11-26 19:41:58 · 375 阅读 · 0 评论 -
实现调用函数功能
1.使用list实现依次调用函数功能def check_network(): print "This is check_network"def check_config(): print "This is check config"def check_io(): print "This is check io"if __name__ == '__main_...原创 2018-08-10 17:19:21 · 547 阅读 · 0 评论 -
static and class method
the different between static method and class methodclass A(object): @classmethod def func1(self, *args): print "This is func1 of A" @staticmethod def func2(*args): ...原创 2018-08-07 16:13:38 · 134 阅读 · 0 评论 -
generator
生成器>>> def gen123():... print "yield to 1"... yield 1... print "yield to 2"... yield 2... print "yield to 3"... yield 3... >>> g = gen123()>&原创 2018-08-11 23:27:52 · 123 阅读 · 0 评论 -
使用lambda进行排序
1.使用lambda对list进行排序>>> a = [('a',1),('b',5),('e',4),('f',2)]>>> sorted(a,key=lambda a :a[1])[('a', 1), ('f', 2), ('e', 4), ('b', 5)]方法1.用List的成员函数sort进行排序,在本地进行排序,不返回副本方法2.用b...原创 2018-08-12 21:44:49 · 11474 阅读 · 0 评论 -
根据另一个数组进行排序
一个数组根据另一个数组进行排序a = ['1', '5', '4', '3']b = ['e', 'f', 'q', 'o']print "before sort"print aprint bn = len(a)for i in range(n-1): for j in range(1,n-i): if int(a[j-1]) > int(a[j])...原创 2018-08-12 21:58:55 · 3940 阅读 · 0 评论 -
生成器(generator)和迭代器(Iterator)
迭代器用于从集合中取出元素;而生成器用于“凭空”生成元素。在 Python 中,使用了 yield 的函数被称为生成器(generator)。练习:输出1到10乘以2的值.def gen10(): for i in range(10): yield 2*(i+1)g = gen10()for i in g: print ioutput:2...原创 2018-08-13 09:40:49 · 318 阅读 · 0 评论 -
使用正则表达式re
邮箱可以以数字和字母开头,但是不能以下划线开头,以.com结尾,返回邮箱的个数import restr1 = '[email protected]@[email protected]'reg_str1 = r'([a-zA-Z0-9]+[\.\w]*@[\w]+\.com)'mod = re.compile(reg_str1)items = re.findall(reg_st...原创 2018-08-13 18:42:06 · 251 阅读 · 0 评论 -
compress
筛选序列中的大于0元素In [1]: from itertools import compressIn [2]: a = ['s', 'v', 'x']In [3]: b = [1, 0, 7]In [4]: more0 = [n > 0 for n in b]In [5]: more0Out[5]: [True, False, True]In [6]: list...原创 2018-08-25 15:04:49 · 422 阅读 · 0 评论 -
namedtuple
通过名称而不是index来访问元素from collections import namedtupleIn [20]: S = namedtuple('S', ['name', 'id'])In [21]: sub = S('wuwei', '631')In [22]: subOut[22]: S(name='wuwei', id='631')In [23]: sub.nam...原创 2018-08-25 15:33:10 · 725 阅读 · 0 评论 -
装饰器实现单例模式
使用装饰器实现单例程模式def singleton(cls, *args, **kw): instances = {} def _singleton(): if cls not in instances: instances[cls] = cls(*args, **kw) return instances[cls] r...原创 2018-06-24 14:15:06 · 754 阅读 · 0 评论 -
打印程序运行时间
用装饰器实现打印程序运行时间import timedef print_run_time(func): def wrapper(*args, **kw): start = time.time() func(*args, **kw) end =time.time() - start print "run time is %s"...原创 2018-06-24 13:54:48 · 736 阅读 · 0 评论 -
subprocess
import subprocess_VERBOSE = Truedef run_cmd(cmd): if _VERBOSE: print cmd process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIP...原创 2018-06-04 16:08:10 · 587 阅读 · 0 评论 -
pdb常用命令
设置断点: b 1设置临时断点:tbreak delete cl(ear) 去掉断点 clear linenum or clear filename:linenum原创 2016-12-30 17:14:23 · 1061 阅读 · 0 评论 -
python解析xml文件
ET.find(path[, namespaces=D])This method is used to find a specific single element in the document. It is essentially equivalent to calling the .find()method on the document's root element; n原创 2016-03-05 20:11:55 · 592 阅读 · 0 评论 -
python学习整理
how to know python run in 32bit or 64bit machineimport ctypesprint ctypes.sizeof(ctypes.c_voidp) It'll be 4 for 32 bit or 8 for 64 bit. urllib库里面有个urlencode函数,可以把key-value这样的键值对转换成我们想要的格式...原创 2015-06-02 10:44:57 · 1175 阅读 · 0 评论 -
P4python sync code from perforce
P4WORKSPACE = "wsx"P4PATH = "/home/s/Perforce/"P4PORT = "perforce-xxx:1800"def _sync_packages(view_common_path):""" sync from perforce"""p4 = P4()p4.port = P4PORTp4.user = "nn"try:原创 2017-04-26 15:16:22 · 1624 阅读 · 0 评论 -
anaconda使用整理
# 创建一个名为python34的环境,指定Python版本是3.4(不用管是3.4.x,conda会为我们自动寻找3.4.x中的最新版本)conda create--namepython34python=3.4 # 安装好后,使用activate激活某个环境activatepython34# for Windowssource activate pytho转载 2018-01-23 10:48:07 · 495 阅读 · 0 评论