ITeye博客

python

共 18 篇文章

GAE发布

很久没有写GAE的东西了,突然想写了点东西,很奇怪突然之间就传不上去了,老是提示需要安装python的ssl的那个东西,每次都提示WARNING appengine_rpc.py:399 ssl module not found. Without the ssl module, the identity of the remote host cannot be verified, and connections may NOT be secure. To fix this, please install the ssl module from http://pypi.python.org/p…

2010-04-24 10:42 · python

django简单的入门例子

django入门例子: 都是参考了网上的例子完成的~ 建立项目:django-admin.py startproject test1 建立目录:django-admin.py startapp views django-admin.py startapp db django-admin.py startapp templates 附件为所有代码 hello.py 如下: #!/usr/bin/env python #-*-coding:utf-8-*- from django.http import HttpResponse from django.template import Conte…

2009-04-30 10:40 · python

摘自python cookbook2(文本文件)

摘自python cookbook2(文本文件) url:http://wiki.woodpecker.org.cn/moin/PyCookbook 1.从文件读取文本或数据 一次将文件内容读入一个长字符串的最简便方法 如:all_the_text = open('thefile.txt').read( ) # 文本文件的全部文本 all_the_data = open('abinfile', 'rb').read( ) # 2进制文件的全部数据 更好的方法是将文件对象和一个变量绑定,可以及时关闭文件。比如,读取文本文件内容: 如:file_object = open('thefile.tx…

2009-04-23 16:11 · python

摘自python cookbook1(字符串,字典)

摘自python cookbook1(字符串,字典) url:http://wiki.woodpecker.org.cn/moin/PyCookbook 1.变量互换值,在java里需要使用临时变量,在python中则可以不必这样,只需 a,b,c=c,a,b这样的即可 2.你需要从字典中获得一个值,不要处理在字典里找不到你所需要的键值的异常。dic.get('key', 'not found') 3.用一个字典D工作,如果D存在,使用它。如果不存在增加一个新的D .d.setdefault(word, ).append(pagenumber) 4.你需要执行一段合适的代码去和一些控制变量的…

2009-04-23 14:55 · python

flup安装问题

在windows下flup一直安装不上,不知道为什么,在linux下面安装一次就成功了~~ 同样的安装方式,网上搜索了半天,都是一笔带过,都没有说怎么在Windows下具体的安装方式,找到一个也碰到我这样的问题的帖子,但没有解决办法,很郁闷耶~~~ 安装方式如下(python版本为2.5): 从网站上下载flup-1.0.1.tar.gz版本,解压缩至c盘,然后进入到解压缩的目录,在dos下执行python setup.py install 命令,执行完后在python环境下输入import flup.server.fcgi_fort ,报错,具体错误如下: import flup.serv…

2009-03-23 19:02 · python

xml的解析例子

xml解析的一个例子,如下所示: #!/usr/bin/env python #-*-coding:utf-8-*- import xml.sax.handler class TestHander(xml.sax.handler.ContentHandler): def __init__(self): self.mapping={} self.content="" #标签的开始 def startElement(self, name, attributes): self.buffer="" #if name=="link": # self.mapping[self.buffer.encode…

2009-03-23 15:34 · python

python备份文件

#-*-coding:utf-8-*- import os import time #备份文件 # 1. The files and directories to be backed up are specified in a list. source = ['/usr/local/apache2', '/usr/local/backup'] # If you are using Windows, use source = [r'c:\documents', r'd:\work'] or something like that # 2. The backup must be stored i…

2009-03-20 18:22 · python

python 解析url

摘录了dive into python的例子 有两种方法,HTMLParser和SGMLParser 第一种: #-*-coding:utf-8-*- import HTMLParser #html解析,继承HTMLParser类 class MyHTMLParser(HTMLParser.HTMLParser): def _init(self): HTMLParser.HTMLParser.__init__(self); # 处理开始标签和结束标签 -- finish processing of start+end tag: <tag.../> def handle_startendtag…

2009-03-20 18:13 · python

使用python下载日志

第一步:主要是读取配置文件的内容,以及把各种数据存放到文件里 #下载日志 def down(): downloadfile=os.path.join(util.FileUtil.getconfig("info", "downloadfile"))#需要下载的文件列表 downloadpath=os.path.join(util.FileUtil.getconfig("info", "downloadpath")) logpath=os.path.join(util.FileUtil.getconfig("info", "logpath")) today=util.DateUtil.getYe…

2009-03-17 18:48 · python

python控制流

条件语句: if expression: statements elif expression: statements elif expression: statements ... else: statements if expression: pass # 不做任何事 else: statements 循环: 第一种while: while expression: statements for i in s: statements 第二种for: for x,y,z in s: statements 异常: try: do something except IOError, e: # 处…

2008-12-21 16:27 · python

python类型转换、数值操作

python类型转换 函数 描述 int(x [,base ]) 将x转换为一个整数 long(x [,base ]) 将x转换为一个长整数 float(x ) 将x转换到一个浮点数 complex(real [,imag ]) 创建一个复数 str(x ) 将对象 x 转换为字符串 repr(x ) 将对象 x 转换为表达式字符串 eval(str ) 用来计算在字符串中的有效Python表达式,并返回一个对象 tuple(s ) 将序列 s 转换为一个元组 list(s ) 将序列 s 转换为一个列表 chr(x ) 将一个整数转换为一个字符 unichr(x ) 将一个整数转换为Uni…

2008-12-21 16:20 · python

python学习之类型和对象

序列是由非负整数索引的对象的有序集合。它包括字符串、Unicode字符串、列表、元组、xrange对象以及缓冲区对象。字符串和缓冲区对象是字符序列,xrange对象是整数的序列,列表和元组是任意Python对象的序列。字符串、Unicode字符串及元组是不可变序列,列表是可变序列,允许插入,删除,替换元素等操作。缓冲区对象将在本节后面详细描述。 Table 3.2列出所有序列对象均支持的操作及方法。序列 s 中的元素 i 使用索引运算符 s* 来访问,通过切片运算符 s 可以得到一个序列的子序列(这些运算符在第四章有详细介绍)。内建函数 len(s) 可以返回任意序列 s 的长度。你还能使用…

2008-12-21 16:14 · python

Django 的数据库查询

class Blog(models.Model): name = models.CharField(max_length=100) tagline = models.TextField() def __unicode__(self): return self.name class Author(models.Model): name = models.CharField(max_length=50) email = models.EmailField() def __unicode__(self): return self.name class Entry(models.Model): bl…

2008-12-14 14:21 · python

摘自python的文档

Boolean Operations — and, or, not Operation Result x or y if x is false, then y, else x x and y if x is false, then x, else y not x if x is false, then True, else False Comparisons Operation Meaning < strictly less than <= less than or equal > strictly greater than >= greater than or equal == equal…

2008-12-11 09:15 · python

python发送email

第一种方法: # -*- coding: utf-8 -*- import email import mimetypes from email.MIMEMultipart import MIMEMultipart from email.MIMEText import MIMEText from email.MIMEImage import MIMEImage import smtplib def sendEmail(authInfo, fromAdd, toAdd, subject, plainText, htmlText): strFrom = fromAdd strTo = ', '.j…

2008-11-11 15:46 · python

python的简单文件操作

#-*-coding:utf-8-*- import os import sys import urllib # def urlread(source): return urllib.urlopen(source).read() #print feeddata #文件读取 def fileread(file_path): # infile=open(file_path,"r") infile=file(file_path,"r") content=infile.read() infile.close() return content #读取行 中文读取出来是转码 返回list def rea…

2008-10-28 09:15 · python

python的数据库链接

附件中是添加到mysql的windows下的库安装程序 #-*-coding:utf-8-*- import MySQLdb #yum update python linux下更新python的命令 #获取数据库连接 def getConn(): host="192.168.0.237" username="root" pwd="root" database="cn_ad_issue" return MySQLdb.connect(host=host,user=username,passwd=pwd,db=database) #根据sql查询数据库,获取的数据是tuple类型 def get…

2008-10-28 09:13 · python

用python实现的时间函数

#-*-coding:utf-8-*- import datetime, calendar def getYesterday(): today=datetime.date.today() oneday=datetime.timedelta(days=1) yesterday=today-oneday return yesterday def getToday(): return datetime.date.today() #获取给定参数的前几天的日期,返回一个list def getDaysByNum(num): today=datetime.date.today() oneday=date…

2008-10-28 09:11 · python