易截截图软件、单文件、免安装、纯绿色、仅160KB
热门标签: c c# c++ asp asp.net linux php jsp java vb Python Ruby mysql sql access Sqlite sqlserver delphi javascript Oracle ajax wap mssql html css flash flex dreamweaver xml
 最新文章 : Python

python中%符号详解

%a 星期几的简写
%A 星期几的全称
%b 月分的简写
%B 月份的全称
%c 标准的日期的时间串
%C
年份的后两位数字
%d 十进制表示的每月的第几天
%D 月/天/年
%e 在两字符域中,十进制表示的每月的第几天
%F
年-月-日
%g 年份的后两位数字,使用基于周的年
%G 年分,使用基于周的年
%h 简写的月份名
%H
24小时制的小时
%I 12小时制的小时
%j 十进制表示的每年的第几天
%m 十进制表示的月份
%M
十时制表示的分钟数
%n 新行符
%p 本地的AM或PM的等价显示
%r 12小时的时间
%R
显示小时和分钟:hh:mm
%S 十进制的秒数
%t 水平制表符
%T 显示时分秒:hh:mm:ss
%u
每周的第几天,星期一为第一天 (值从0到6,星期一为0)
%U 第年的第几周,把星期日做为第一天(值从0到53)
%V
每年的第几周,使用基于周的年
%w 十进制表示的星期几(值从0到6,星期天为0)
%W
每年的第几周,把星期一做为第一天(值从0到53)
%x 标准的日期串
%X 标准的时间串
%y
不带世纪的十进制年份(值从0到99)
%Y 带世纪部分的十制年份
%z,%Z 时区名称,如果不能得到时区名称则返回空字符。
%%
百分号
......

关于Python中时间与字符串直接的转换

>>> import time
>>> import datetime
>>>
now = time.localtime()
>>> now
(2006, 4, 30, 18, 7, 35,
6, 120, 0)
>>> type(now)
<type 'time.struct_time'>
>>>
str_now = time.strftime("%m/%d/%Y %X", now )
>>>
str_now
'04/30/2006 18:07:35'
>>> new_now =
time.strptime( str_now, "%m/%d/%Y %X" )
>>> new_now
(2006,
4, 30, 18, 7, 35, 6, 120, -1)
这里,strftime 将 struct_time 的时间按指定的格式转化成 字符串
strptime 将 字符串按指定的格式转化成
struct_time 的时间
struct_time 的时间没有现成的比较的函数,可以将 struct_time 转化成 datetime.datetime
>>> d_now = datetime.datetime( *now[:6] )
>>>
type(d_now)
<type 'datetime.datetime'>
>>> d_now
datetime.datetime(2006,
4, 30, 18, 7, 35) ......

python读取目录下文件并生成日志

很长的一段代码,但很清楚。哈哈。
import os
from time import strftime
stamp=strftime("%Y-%m-%d %H:%M:%S")
logfile = 'F:\\test\\m-php-framework\\tmp\logs\\error_report.log'
path = 'F:\\test\\'
files = os.listdir(path)
bytes = 0
numfiles = 0
for f in files:
if f.startswith('t'):
info = os.stat(path + f)
numfiles +=1
bytes+=info[6]
if numfiles > 1:
title = 'tiles'
else:
title = 'file'
string = stamp + " -- " + str(numfiles) + " session" +title+","+ str(bytes)+" bytes\n"
file = open(logfile,"a")
file.writelines(string)
file.close()
......

如何在Apache下设置Python(WIN,CGI方法)

关于Python,本人不想多说了,如果不知道什么是Python,可以看《什么是Python
?》一文。Python可以开发CGI程序,那么在Apache下应如何配置呢?本文只讲述了如何
以CGI方式配置Apache,使其支持Python程序。其它方式,如mod_python则不在讨论范围
(其实是没配成功:-)。我所使用的系统环境为Windows 2000, Apache 1.3.19,Python
2.1版。
准备
  首先检查以下要求是否已经达到:
Apache已经安装,并且可以正常使用
Python已经安装,并且可以正常使用(在我的环境下,Python安装目录为c:\servers\py
thon)
  好,如果一切正常,下面就开始了。
配置
  修改DocumentRoot
打开Apache安装目录下的conf子目录的httpd.conf文件。可以修改DocumentRoot为"C:/
Inetpub/wwwroot"。当然你可以按需要改成其它值。
  允许任意目录执行CGI
  这个设置是允许被设目录及其子目录下的CGI程序可以CGI方式运行。在Apache中,
尽管你可能已经设置了CGI文件后缀,但是如果未设置允许CGI程序运行选项,则无法运
行CGI程序。
  设置C:/Inetpub/wwwroot目录属性:
<Directory "C:/Inetpub/wwwroot">
Options Indexes FollowSymL ......

Python MySQLdb 查询返回字典结构


Python MySQLdb 查询返回字典结构 smallfish
MySQLdb默认查询结果都是返回tuple,输出时候不是很方便,必须按照0,1这样读取,无意中在网上找到简单的修改方法,就是传递一个cursors.DictCursor就行。
默认程序:
import MySQLdb
db = MySQLdb.connect(host = 'localhost', user = 'root', passwd = '123456', db = 'test')
cursor = db.cursor()
cursor.execute('select * from user')
rs = cursor.fetchall()
print rs
# 返回类似如下
# ((1000L, 0L), (2000L, 0L), (3000L, 0L))
修改后:
import MySQLdb
import MySQLdb.cursors
db = MySQLdb.connect(host = 'localhost', user = 'root', passwd = '123456', db = 'test',
cursorclass = MySQLdb.cursors.DictCursor)
cursor = db.cursor()
cursor.execute('select * from user')
rs = cursor.fetchall()
print rs
# 返回类似如下
# ({'age': 0L, 'num': 1000L}, {'age': 0L, 'num': 2000L}, {'age': 0L, 'num': 3000L})
或者也可以用下面替换connect和cursor部分
db = MySQLdb.connect(host = 'localhost', user = 'root', passwd = '123456', db = 'test')
cursor ......

Python 处理excel并转为table

使用xlrd

读取,xlrd的下载及安装可以参看:
Python
"xlrd" package for extracting data from Excel files
---------------------------------------------------------------------------------
#coding=utf-8
import xlrd
import os, types, datetime
#excel存放目录
dir = u'D:\\temp\\excel'
#生成html存放目录,需要事先建立
htmlroot = u'D:\\temp\\html'
#excel从1899/12/31计算时间
__s_date = datetime.date(1899, 12, 31).toordinal() - 1
#读取时间格式的单元
def getdate(date):
    if isinstance(date, float):
        date = int(date)
    d = datetime.date.fromordinal(__s_date + date)
    return d.strftime("%Y-%m-%d")
#遍历文件夹
for root, dirs, files in os.walk(dir):
    for file in files:
        #生成excel文件路径
        path = os.path.join(root, file)
  ......
总记录数:695; 总页数:116; 每页6 条; 首页 上一页 [8] [9] [10] [11] 12 [13] [14] [15] [16] [17]  下一页 尾页
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号