摘要:#设a为字符串import timea = "2011-09-28 10:00:00"#中间过程,一般都需要将字符串转化为时间数组time.strptime(a,'%Y-%m-%d %H:%M:%S')>>time.struct_time(tm_year=2011, tm_mon=9, tm_mday=27, tm_hour=10, tm_min=50, tm_sec=0, tm_wday=1, tm_yday=270, tm_isdst=-1)#将"2011-09-28 10:00:00"转化为时间戳time.mktime(
阅读全文
摘要:import stringdef Translator(frm = '', to = '', delete = '', keep = None): if len(to) == 1: to = to * len(frm) trans = string.maketrans(frm, to) if keep is not None: allchars = string.maketrans('', '') delete = allchars.translate(allchars, keep.translate(allcha
阅读全文
摘要:来源:http://www.cnblogs.com/zhangpengshou/archive/2012/03/12/2392068.html#!/usr/bin/env python# -*- coding: utf-8 -*-# 2/10/16 base trans. wrote by srcdog on 20th, April, 2009# ld elements in base 2, 10, 16.import os,sys# global definition# base = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F]base =
阅读全文
摘要:来源:Python参考手册要将程序的输出送到一个文件中,需要在 print 语句后面使用 >> 指定一个文件,如下所示:principal = 1000 # 初始金额rate = 0.05 # 利率numyears = 5 # 年数year = 1f = open("out.txt", "w") # 打开文件以便写入while year <= numyears: principal = principal * (1 + rate) ...
阅读全文
摘要:代码来源: Python参考手册f = open("foo.txt") # 返回一个文件对象line = f.readline() # 调用文件的 readline()方法while line: print line, # 后面跟 ',' 将忽略换行符 # print(line, end = '') # 在 Python 3中使用 line = f.readline()f.close()也可以写成以下更简洁的形式for line in open("foo.txt"): print lin...
阅读全文