python 解析页面的类
from HTMLParser import HTMLParser class MyHTMLParser(HTMLParser): def __init__(self): HTMLParser.__init__(self) self.flag = 0 # lable flag self.content_flag= 0 #content flag self.links = [] self.title=”” self.img=”” self.content=”” def handle_starttag(self, tag, attrs): #print “Encountered the beginning of a %s tag” % tag if tag == “a”: if len(attrs) == 0: pass else: for (variable, value) in attrs: if [...]
jinja2 模板显示格式化的时间(时间戳)
在jinja2的filters文件中先import time,然后先写个格式化的方法: def do_datetimeformat(value, format=’%Y-%m-%d %H:%M:%S’): ”’格式化输出日期”’ return time.strftime(format, time.localtime(value)) 最后在 FILTERS里面把do_datetimeformat包含了
python 如何解析json数据
一、JSON的格式: 1,对象: {name:”Peggy”,email:”peggy@gmail.com”,homepage:”http://zhanghui.me”} { 属性 : 值 , 属性 : 值 , 属性 : 值 } 2,数组是有顺序的值的集合。一个数组开始于“[",结束于"]“,值之间用“,”分隔。 [ {name:"Peggy",email:"peggy@gmail.com",homepage:"http://zhanghui.me"}, {name:"Peggy",email:"peggy@gmail.com",homepage:"http://zhanghui.me"}, {name:"Peggy",email:"peggy@gmail.com",homepage:"http://zhanghui.me"} ] 3, 值可以是字符串、数字、true、false、null,也可以是对象或数组。这些结构都能嵌套。 4,json示例: import json # Converting Python to JSON json_object = json.write( python_object ) # Converting JSON to Python python_object = json.read( json_object ) 5,simplejson 示例: import simplejson # Converting Python to JSON json_object = simplejson.dumps( python_object ) # Converting JSON to Python python_object = simplejson.loads( json_object ) [...]
安装配置Django开发环境(Eclipse + Pydev)
一、安装Eclipse SDK。 到 http://download.eclipse.org/eclipse/downloads/ 找SDK最新的Release版本,最新的版本是 3.3.2 下载。一开始我选择的是台湾的[Taiwan] National Center for High-Performance Computing (http) ,速度只有5kB/s,还是小日本的快,118kB/S,, Japan Advanced Institute of Science and Technology (http) ,做事情应该也这样,一个明显很缓慢,就要当即力断换其他的。 下载完毕之后,将eclipse SDK解压到某个目录下,我的放在e:\eclipse就可以了。 如果要使用中文的,最好是使用跟SDK配套的语言包,否则也不能完全汉化(最后我选择了纯英文的,没汉化)。根据网上的资料,汉化前不要启动Eclipse,否则不能完全汉化。将语言包也解压到同一目录下即可。 二、安装Pydev 到 http://sourceforge.net/projects/pydev/ 这里找最新版本下载,最新版本是1.3.15 下载后解压,将plugins目录下所有文件挪到Eclipse的相应目录下,将feature目录下所有文件挪到Eclipse的相应目录下,最后启动Eclipse,在Help-》Software Upates-》Manage Configuration,然后弹出的界面即可看到pydev的插件。 另一种安装插件的方法是:Help-》Software Upates-》Find and Install-》Search for new features to install-》New remote site-》随便起个name如pydev,url填http://pydev.sf.net/updates/ -》然后照着提示下载安装即可。 两种方法我都尝试了一下,为保险起见,我选择的是第二种方法。 三 、Pydev的配置 在Eclipse IDE 下, 打开 Window->Preferences… 对话框,从右侧的树形列表中选择“ PyDev”->“Interpreter Python”, 点击New按钮,从Python的安装路径下选择Python.exe,然后会弹出一个对话框让你勾选System PYTHONPATH,我是都选了,应该无所谓。最后点Ok,退出。 四、进行Django项目开发 1、建立PyDev [...]
Python中序列化处理之——marshal和cPickle篇
#!/usr/bin/python #coding=utf-8 import os import marshal,cPickle “”" marshal只能序列化有限的类型 而cPickle能够序列化自定义的类型 “”" class Foo: def __init__(self, name): self.name = name def __str__(self): return self.name o=range(0,10) L = Foo(“Goodspeed”) cls = [marshal,cPickle] for c in cls: print c.__name__ #序列化到文件中 fi =open(os.path.join(os.getcwd(),’fle.txt’),’wb’) c.dump(o,fi) fi.close() fi =open(os.path.join(os.getcwd(),’fle.txt’),’rb’) o1 = c.load(fi) fi.close() #序列化成字符串 d = c.dumps(o) o2 = c.loads(d) print o [...]
Python中时间的处理之——calendar篇
#! /usr/bin/python # coding=utf-8 import calendar “”" 返回的某个月的日历 返回类型是字符串型 “”" cal = calendar.month(2011, 11) “”" 返回一年的日历 “”" cal = calendar.calendar(2011) cal = calendar.HTMLCalendar(calendar.MONDAY) “”" 打印出一个月日历 “”" cal.formatmonth(2011, 11) “”" 打印出一年的日历 formatyearpage将生成完整的页面代码 “”" print cal.formatyear(2011) cal.formatyearpage(2011) “”" 默认每周的第一天是星期一,这里修改成星期天 “”" calendar.setfirstweekday(calendar.SUNDAY)
Python中时间的处理之——tzinfo篇
#! /usr/bin/python # coding=utf-8 from datetime import datetime, tzinfo,timedelta “”" tzinfo是关于时区信息的类 tzinfo是一个抽象类,所以不能直接被实例化 “”" class UTC(tzinfo): “”"UTC”"” def __init__(self,offset = 0): self._offset = offset def utcoffset(self, dt): return timedelta(hours=self._offset) def tzname(self, dt): return “UTC +%s” % self._offset def dst(self, dt): return timedelta(hours=self._offset) #北京时间 beijing = datetime(2011,11,11,0,0,0,tzinfo = UTC(8)) #曼谷时间 bangkok = datetime(2011,11,11,0,0,0,tzinfo = UTC(7)) #北京时间转成曼谷时间 beijing.astimezone(UTC(7)) [...]
Python中时间的处理之——date和time篇
#! /usr/bin/python # coding=utf-8 from datetime import datetime,date,time “”" date类型顾名思义就是只表示日期,而time只表示time “”" today = date.today() attrs = [ ("year","年"),( 'month',"月"),( 'day',"日") ] for k,v in attrs: “today.%s = %s #%s” % (k,getattr(today, k),v) #星期几。同datetime规则一样 today.isoweekday() today.weekday() #返回一个time结构,当然也就没有时间信息 today.timetuple() #修改。同datetime规则一样 today.replace(month=1) #转成字符串,不可以转回 today.strftime(“%Y-%m-%d”) now = time(12,13,14) attrs = [ ("hour","小时"),( 'minute',"分"),( 'second',"秒"),("microsecond","毫秒") ] for k,v in attrs: [...]
Python中时间的处理之——Time篇
#! /usr/bin/python # coding=utf-8 import time from datetime import datetime “”" 表示日常所用时间的类,是用C实现的内嵌类。 功能比较简单,但效率高。表示的时间范围有限1970年1月1日到2038年1月19日。 “”" “”" 当前时间 返回的一个float型,以一个固定时间epoch(1970年1月1日0时起经过的秒数) 因为time终究是以float型来表示的,所以对于timespan的问题,基本就成了数字问题。 “”" now = time.time() “”" 使用localtime 返回一个time结构, 其中包括tm_year,tm_mon,tm_mday,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst=0 夏令时间标志 tm_wday为周几,0是周一,6是周日 “”" now = time.localtime(now) #如果是返回当前时间,可以简单的写成 time.localtime() #这个返回UTC时间 time.gmtime() “”" 转成字符串 %y 两位数的年份表示(00-99) %Y 四位数的年份表示(000-9999) %m 月份(01-12) %d 月内中的一天(0-31) %H 24小时制小时数(0-23) %I 12小时制小时数(01-12) %M 分钟数(00=59) %S 秒(00-59) %a [...]
Python中时间的处理之——datetime篇
#! /usr/bin/python # coding=utf-8 import datetime “”" datetime的功能强大 能支持0001年到9999年 “”" “”" 当前时间 返回的是一个datetime类型 now方法有个参数tz,设置时区类型。如果没有和方法today的效果一样 “”" now = datetime.datetime.now() #UTC时间 datetime.datetime.utcnow() attrs = [ ("year","年"),('month',"月"),("day","日"),('hour',"小时"),( 'minute',"分"),( 'second',"秒"),( 'microsecond',"毫秒"),( 'min',"最小"),( 'max',"最大"), ] for k,v in attrs: “now.%s = %s #%s” % (k,getattr(now, k),v) “”" 返回一个time结构 “”" now.timetuple() “”" 返回一个date类型 http://www.cnblogs.com/goodspeed/archive/2011/11/07/python_date_time.html “”" now.date() “”" 返回一个time类型 http://www.cnblogs.com/goodspeed/archive/2011/11/07/python_date_time.html “”" now.time() [...]