python提供了非常方便的日志模块#-*- coding:utf-8 -*-import logging# 配置日志信息logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)-12s %(levelname)-……继续阅读 » 水墨上仙 7年前 (2019-09-03) 2107浏览 1798个赞
split(string[, maxsplit]) | re.split(pattern, string[, maxsplit]):按照能够匹配的子串将string分割后返回列表。maxsplit用于指定最大分割次数,不指定将全部分割。import re p = re.compile(r'\d+')print p.spli……继续阅读 » 水墨上仙 7年前 (2019-09-03) 2274浏览 2432个赞
python 中map函数使用范例代码# build a dictionary that maps the ordinals from 32 to 255# to their ASCII character equivalents eg. 33: '!'# (note that 32 and 160 are spaces)……继续阅读 » 水墨上仙 7年前 (2019-09-03) 1732浏览 1777个赞
python统计文本文件内单词数量# count lines, sentences, and words of a text file# set all the counters to zerolines, blanklines, sentences, words = 0, 0, 0, 0print '-' * 50tr……继续阅读 » 水墨上仙 7年前 (2019-09-03) 3369浏览 1589个赞
python计算数组、元祖等列表元素的和print( sum([1,2,3])) 返回值:6……继续阅读 » 水墨上仙 7年前 (2019-08-08) 2620浏览 773个赞
python遍历字符串中的字符word = raw_input("Enter a word: ")print "\nHere's each letter in your word:"for letter in word: print letter……继续阅读 » 水墨上仙 7年前 (2019-08-08) 2918浏览 2287个赞
def isAString(anobj):return isinstance(anobj, basestring)def isAString(anobj): return isinstance(anobj, basestring)……继续阅读 » 水墨上仙 7年前 (2019-08-08) 2155浏览 1440个赞
multipart/form-data类型的POST实体结构相对来说(常规的POST正文采用application/x-www-form-urlencoded格式)比较复杂,它常用于文件上传。下面是一个multipart/form-data格式的POST实体示例-----------------------------114782935826962 ……继续阅读 » 水墨上仙 7年前 (2019-08-08) 1842浏览 1541个赞
python写日志的封装类# encoding:utf-8import sysimport loggingimport time def writeLog(message): logger=logging.getLogger() filename = time.strftime('%Y-%m-%d'……继续阅读 » 水墨上仙 7年前 (2019-08-08) 3246浏览 504个赞
python链接Oracle数据库的代码,需要引用cx_Oracle库#coding=UTF-8 import cx_Oracle def hello(): '''Hello cx_Oracle示例: 1)打印数据库版本信息. 2)查询表数据.'……继续阅读 » 水墨上仙 7年前 (2019-08-08) 1708浏览 1290个赞
python 给目录下的图片批量加水印water.py 放到 图片文件夹里 然后cd 到当前文件夹 python water.py#coding=utf-8import Imageimport os#print list[0]#exit()def getlogo(x1,y1): im =Image.open("./&qu……继续阅读 » 水墨上仙 7年前 (2019-08-08) 2181浏览 2965个赞
python正则查找所有匹配的字符串import re p = re.compile(r'\d+')print p.findall('one1two2three3four4') ### output #### ['1', '2', '3……继续阅读 » 水墨上仙 7年前 (2019-08-08) 3628浏览 2905个赞
python zip和unzip数据# zipping and unzipping a string using the zlib module# a very large string could be zipped and saved to a file speeding up file writing time # and later rel……继续阅读 » 水墨上仙 7年前 (2019-08-08) 2676浏览 1951个赞
wxpython GUI界面显示jpg图片# show a jpeg (.jpg) image using wxPython, newer coding style# two different ways to load and display are given# tested with Python24 and wxPython25 veg……继续阅读 » 水墨上仙 7年前 (2019-08-08) 1523浏览 1283个赞
python将文本转换成语音# Text To Speech using SAPI (Windows) and Python module pyTTS by Peter Parente# download installer file pyTTS-3.0.win32-py2.4.exe # from: http://sourceforge.ne……继续阅读 » 水墨上仙 7年前 (2019-08-08) 3282浏览 414个赞
由于Python设计的限制(我说的是咱们常用的CPython)。最多只能用满1个CPU核心。Python提供了非常好用的多进程包multiprocessing,你只需要定义一个函数,Python会替你完成其他所有事情。借助这个包,可以轻松完成从单进程到并发执行的转换。1、新建单一进程如果我们新建少量进程,可以如下:import multiproces……继续阅读 » 水墨上仙 7年前 (2019-08-08) 2408浏览 2051个赞
在2.6才开始使用multiprocessing 是一个使用方法类似threading模块的进程模块。允许程序员做并行开发。并且可以在UNIX和Windows下运行。通过创建一个Process 类型并且通过调用call()方法spawn一个进程。下面是该模块的一个测试程序。效果非常好#!/usr/bin/env python#coding=utf-……继续阅读 » 水墨上仙 7年前 (2019-08-08) 1751浏览 1391个赞
python subprocess模块 监控子进程的2种方式 忙等待和立即返回同时设置子进程超时时间一:循环 忙等 子进程结束import subprocess import os import time tt = '555' cmd = "python /home/1……继续阅读 » 水墨上仙 7年前 (2019-08-08) 3015浏览 2253个赞
python插入排序示范代码(算法)插入排序就是每一步都将一个待排数据按其大小插入到已经排序的数据中的适当位置,直到全部插入完毕. 数据演示:[1,4,2,3]第一次:[1,4,2,3]第二次:[1,4,2,3]第三次:[1,2,4,3]第四次:[1,2,3,4]代码示例如下:……继续阅读 » 水墨上仙 7年前 (2019-08-08) 2861浏览 797个赞
Advanced Python Scheduler是一个python的定时执行任务的框架,调用无比简单,不用自己写定时器了 演示代码from apscheduler.scheduler import Schedulersched = Scheduler()@sched.i……继续阅读 » 水墨上仙 7年前 (2019-08-08) 2299浏览 1563个赞
python重命名文件代码#-*-coding:gbk-*-import oscur_path = os.getcwd()lists = os.listdir( cur_path )for f in lists: if f != 'ren.py': print cur_path ……继续阅读 » 水墨上仙 7年前 (2019-08-08) 2538浏览 2093个赞
python通过xmlrpc进行远程调用的范例演示服务器端代码# -*- coding: utf-8 -*-import SimpleXMLRPCServer#server 上面的程式碼 def Show_me_some_message(sMsg):#從遠端呼叫並且帶入參數 print "I see your call……继续阅读 » 水墨上仙 7年前 (2019-08-08) 3430浏览 991个赞
一个Python编写的简单hangman游戏代码#!/usr/bin/env pythonimport random import cPickle class Hangman(object): '''A simple hangman game that tries to improve your voca……继续阅读 » 水墨上仙 7年前 (2019-08-03) 2425浏览 229个赞
python执行子进程 进程间通信a.pyimport subprocess, timesubproc = subprocess.Popen(['c:\python31\python.exe', 'c:/b.py'], stdin=subprocess.PIPE, shell=True) time.sl……继续阅读 » 水墨上仙 7年前 (2019-08-03) 3368浏览 1352个赞
python rpc twisted 服务端和客户端代码演示#服务器端代码如下from twisted.web import xmlrpc, serverclass Example(xmlrpc.XMLRPC): """ An example object to be published. ……继续阅读 » 水墨上仙 7年前 (2019-08-03) 2317浏览 2015个赞
wxpython自定义语法高亮控件,支持python语法高亮,只需要稍加修改就可以支持其它语言的语法高亮import keywordimport wximport wx.stc as stcimport images#--------------------------------------------------------……继续阅读 » 水墨上仙 7年前 (2019-08-03) 3280浏览 1224个赞
python集合使用范例# sets are unordered collections of unique hashable elements# Python23 tested vegaseat 09mar2005# Python v2.4 has sets built inimport setsprint "……继续阅读 » 水墨上仙 7年前 (2019-08-02) 3392浏览 981个赞
python读写ini配置文件import ConfigParserimport osclass ReadWriteConfFile: currentDir=os.path.dirname(__file__) filepath=currentDir+os.path.sep+"inetMsgConfigure.ini&……继续阅读 » 水墨上仙 7年前 (2019-08-02) 3508浏览 2248个赞
最近在做SEO的时候,为了让发的外链能够快速的收录,想到了利用ping的功能,google和百度都有相关的ping介绍,有兴趣的朋友可以去看看相关的知识。实现ping功能除了可以用一些开源的博客程序,比如WP,它是可以在后台设置ping地址的,只要设置好以后,你发帖子,就会自动的通知搜索引擎,我的博客已经更新了,而今天我用的方法是不通过WP等带有ping功能……继续阅读 » 水墨上仙 7年前 (2019-08-02) 2844浏览 664个赞
python通过tarfile模块压缩文件夹import tarfileimport osimport sysuser = os.getenv('USERNAME')filename = '/home/%s/tmp.tgz' % userprint 'The tar file was ……继续阅读 » 水墨上仙 7年前 (2019-08-02) 3195浏览 1654个赞
python 创建gtk应用程序,需要安装gtk包#!/usr/bin/env python## [代码名字: Create an Application Indicator]# [代码分类: Application Indicator]# [代码描述: How to create an application indicator and ad……继续阅读 » 水墨上仙 7年前 (2019-07-11) 2690浏览 1256个赞
我们知道python里没有switch语句,只能用多个if语句来实现,但python的字典却是万能的,下面的代码用字典实现了类似switch的功能try: sql_type = { 'STRING': 'TEXT', 'DOUBLE': 'NU……继续阅读 » 水墨上仙 7年前 (2019-07-11) 2529浏览 1628个赞
python计算代码执行时间import time start = time.time() '''code you want to time goes here''' end = time.time()elapsed = end - startprint "Time ……继续阅读 » 水墨上仙 7年前 (2019-07-11) 3160浏览 2347个赞
python在当前文件夹下设置一个简单的http服务器,执行后可以直接访问:http://127.0.0.1:8000就可以看到当前文件夹下的文件列表python -m SimpleHTTPServer……继续阅读 » 水墨上仙 7年前 (2019-07-11) 3169浏览 193个赞
有两个数组,它们长度相同,你希望把两个数组合并成一个字典,第一个数组的作为键,第二个数组作为值,python都可以很简单的帮你实现keys = [1, 2, 3, 4]values = [55, 56, 57, 58] new_dict = dict(zip(keys, values))……继续阅读 » 水墨上仙 7年前 (2019-07-11) 3681浏览 2506个赞
当你在函数定义内声明变量的时候,它们与函数外具有相同名称的其他变量没有任何关系,即变量名称对于函数来说是 局部 的。这称为变量的 作用域 。所有变量的作用域是它们被定义的块,从它们的名称被定义的那点开始。#!/usr/bin/python# Filename: func_local.pydef func(x): print 'x ……继续阅读 » 水墨上仙 7年前 (2019-07-11) 1870浏览 1602个赞
一段用来判断图片是否是色情图片的python代码,安装了pil包既可以使用转自:http://blog.csdn.net/lanphadayimport sys, Image pic = '2.jpg'img = Image.open(pic).convert('YCbCr') w, h = img.……继续阅读 » 水墨上仙 7年前 (2019-07-11) 2968浏览 105个赞
python链接字符串的几种方法 方法1:直接通过加号操作符相加foobar = ‘foo’ + ‘bar’ 方法2:join方法list_of_strings = ['abc', &……继续阅读 » 水墨上仙 7年前 (2019-07-11) 1708浏览 1778个赞
python添加字符到文件代码with open("filename","a") as out: out.write("aString") ……继续阅读 » 水墨上仙 7年前 (2019-07-11) 2555浏览 541个赞
子类化wxPython application类class MyApp(wx.App): def OnInit(self): frame = wx.Frame(parent=None, id=-1, title=”Bare”) frame.Show() return True……继续阅读 » 水墨上仙 7年前 (2019-07-11) 1459浏览 2509个赞
python中id()函数的实用研究实例>>> a = 2.5 >>> b = 2.5 >>> c = b >>> a is c 输出:False >>> a ……继续阅读 » 水墨上仙 7年前 (2019-07-11) 1658浏览 2575个赞
在编码,解码过程中,会出现某些字符始终无法处理,此时该字符出现在(‘[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]’)范围值呢,去掉这些字符即可顺利编码,import re s = re.compile('[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]').s……继续阅读 » 水墨上仙 7年前 (2019-07-11) 3262浏览 2637个赞
一个python高强度密码生成器,可以指定要生成的密码长度from os import urandomfrom random import choicechar_set = {'small': 'abcdefghijklmnopqrstuvwxyz', 'nums'……继续阅读 » 水墨上仙 7年前 (2019-07-11) 2374浏览 1802个赞
下面的代码可以从网络读取一张图片,不需要保存为本地文件,直接通过Image模块对图片进行处理,这里使用到了cStringIO库,主要是把从网络读取到的图片数据模拟成本地文件。import urllib2import Imageimport cStringIOdef ImageScale(url,size): file = cStringI……继续阅读 » 水墨上仙 7年前 (2019-07-11) 2137浏览 411个赞
用python来清除字符串里的html标签>>> print stripTags(‘Hello, world !‘)Hello, world !def stripTags(s): ''' Strips HTML tags. Taken from http://aspn……继续阅读 » 水墨上仙 7年前 (2019-07-11) 3029浏览 912个赞