xlrd模块
以下的函数是读取形如下图的年月日,并返回日列表

import xlrd
import datetime
def xlrtTime(path):
path = input('请输入Excel文件路径:')
workbook = xlrd.open_workbook(path)
sheet = workbook.sheet_by_index(0)
a = []
t = []
for row in range(sheet.nrows):
if row >0:
rows = sheet.row(row)
for col in range(sheet.ncols):
a.append(rows[4].value)
d=datetime.datetime(int(rows[1].value),int(rows[2].value),int(rows[3].value))
d.strftime('%Y/%m/%d')
t.append(d)
return t
以下是画t-a时序图,其中t为列表,元素类型均为datetime.datetime
#import matplotlib.dates as mdates
#import matplotlib.pyplot as plt
def pltTime(a,t):
xs = t
ys = a
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%Y'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
plt.plot(xs, ys)
plt.gcf().autofmt_xdate() # 自动旋转日期标记
plts = plt.show()
return plts
reduce&map
map(func,seq1[,seq2...])
:将函数func作用于给定序列的每个元素,并用一个列表来提供返回值;如果func为None,func表现为身份函数,返回一个含有每个序列中元素集合的n个元组的列表。
reduce(func,seq[,init])
:func为二元函数,将func作用于seq序列的元素,每次携带一对(先前的结果以及下一个序列的元素),连续的将现有的结果和下一个值作用在获得的随后的结果上,最后减少我们的序列为一个单一的返回值:如果初始值init给定,第一个比较会是init和第一个序列元素而不是序列的头两个元素。
详情见:https://zhuanlan.zhihu.com/p/30077152
其他
Python命名规范:驼峰命名,https://unbug.github.io/codelf/
Python面向对象:封装、多态、继承
Python之%s%d%f
print( "string=%2s" % string)
#%2s意思是字符串长度为2,当原字符串的长度超过2时,按原长度打印
print( "string=%-2s!" % string)
#%-2s意思是字符串长度为2,当原字符串的长度小于2时,在原字符串右侧补空格
print ("string=%.2s" % string)
#%.2s意思是截取字符串的前2个字符
print( "string=%*.*s" % (7,2,string))
#还可以用%*.*s来表示精度,两个*的值分别在后面小括号的前两位数值指定
ps
接下来准备完善一下,这个脑图

分享几张网上找来的脑图



网友评论