代码说话:

# -*- coding: cp936 -*-import os
import re
import sys
import chardet
import fnmatch
import win32com.client
import numpy as np
import matplotlib.pyplot as plt
#显示中文包
from matplotlib.font_manager import FontProperties#获取绝对路径PATH = os.path.abspath(os.path.dirname(sys.argv[0]))"""
使用到的变量
"""first_f = 0
first_month = 0
first_year = 0
file_cnt = 0
all_money = 0
#week_money = [0]
week_money = [1342.8, 401.2, 158.0, 250.0, 870.0,117.2, 860.4, 240.8, 283.3, 488.8,442.5, 331.4, 337.12, 742.7, 638.2,430.0, 253.2, 130.3, 614.5, 450.1,198.8, 221.2, 324.9, 979.02, 170.8,204.0, 560.3, 1106.3, 126.3, 639.6,832.7, 631.0, 888.5, 952.7, 475.8,751.0, 130.0, 459.1, 190.5, 1127.3,308.5, 152.5, 844.0, 1394.4, 319.8,1172.3, 789.51, 1277.3, 277.2, 742.3,467.6, 580.7, 1263.4, 570.9, 381.5,670.7, 607.5, 1219.0, 381.2, 398.0,1132.5, 234.21, 701.4, 1160.1, 460.6,353.4, 375.3, 137.0, 100.4, 724.2,422.8, 684.4, 605.4, 679.3, 120.5,159.5, 915.5, 965.5, 346.5, 254.5,466.0, 1171.2, 190.0, 1075.7, 234.8,198.79, 762.74, 332.57, 224.5, 207.0,963.8, 750.44, 188.0, 624.1, 331.5,473.1, 164.8, 207.5, 187.5, 135.5]new_month = [0]
month_money = [0]
basic_month = [u'2月',u'3月',u'4月',u'5月',u'6月',u'7月',u'8月',u'9月',u'10月',u'11月',u'12月']"""
绘制花费曲线
"""
def money_plot():#使用windows系统自带字体font = FontProperties(fname=r"C:\\WINDOWS\\Fonts\\simsun.ttc", size=10)#设置图表1尺寸plt.figure(figsize=(13,9.5))global week_moneyglobal month_moneyglobal new_monthx1 = range(0,len(week_money))y1 = week_money#x2使用条件表达式,不能被4整除时长度要+1x2 = range(0,len(week_money)%4==0 and len(week_money)/4 or len(week_money)/4+1)y2 = month_moneyplt.subplot(211)plt.plot(x1,y1,'r',label="money")plt.plot(x1,y1,'bo')plt.title(u"每周花费统计",fontproperties=font)plt.xlabel(u"周数",fontproperties=font)plt.ylabel(u"金额(¥)",fontproperties=font)plt.grid(True,color='g',linewidth=1)plt.legend()plt.subplot(212)plt.plot(x2, y2,'c',label="money")plt.plot(x2, y2,'bo')plt.xticks(x2,new_month,rotation=40,fontproperties=font)plt.title(u"每月花费统计",fontproperties=font)plt.xlabel(u"月份",fontproperties=font)plt.ylabel(u"金额(¥)",fontproperties=font)plt.grid(True,color='g',linewidth=1)plt.legend()plt.savefig("figure.png",format="png")plt.show()"""
将每周的花费转化为每月的花费
"""new_month = [0]def week_to_month():#声明使用的全局变量global week_moneyglobal month_moneyglobal first_monthglobal first_yearglobal new_monthglobal basic_month#将每周的花费转换为每月的花费sum = 0for i in range(0,len(week_money)):sum += week_money[i]#每4周计算一次月消费if i%4 == 3:if month_money[0] == 0:month_money[0] = round(sum,2)else:month_money.append(round(sum,2))sum = 0#不足一月按一月算if i == len(week_money)-1:if len(week_money)%4 != 0:month_money.append(round(sum,2))sum = 0#print "\n\n",month_money,"\n","total len_month",len(month_money)#计算月份编号表index = first_monthwhile index < len(month_money)+first_month:if new_month[0] == 0:if index == 1:new_month[0] = str(first_year)else:new_month[0] = basic_month[index-2]#偏移为2,固减去2elif index%12 == 0:new_month.append(basic_month[12-2])elif index%12 == 1:new_month.append(str(first_year+index/12))else:                new_month.append(basic_month[index%12-2])index += 1#for i in range(len(new_month)):#print new_month[i].encode('utf8')"""
提取文件中每周的花费并保存下来
"""def read_txt_data():#读取txt文件内容global week_moneyglobal all_moneyglobal file_cntglobal first_fglobal first_monthglobal first_yeartmp_money = 0for root, dirs,files in os.walk(PATH):for _dir in dirs:passfor _file in files:if fnmatch.fnmatch(_file,'*.txt'):work_file = os.path.join(root, _file)f = open(work_file,'r')line = f.readline()print line#输出第一行的日期if first_f == 0: #执行一次:读取第一次的年份和月份,后面依次累加first_f = 1date_data = re.findall(r"\d+\.?\d*",line)first_year = int(date_data[0])/10000first_month = int(date_data[0])%10000/100#print "year",first_year ,"month",first_monthelse:passwhile True:line = f.readline()if line:#打印每一行的内容#print line,content = linestart = content.find("总计")#注意读出来的文本编码方式#判断是否是"总计那一行"if start != -1:#print start#获取位置num = re.findall(r"\d+\.?\d*",content)tmp_money = round(float(num[0]),2)#输出其中的数字if week_money[0] == 0:week_money[0] = tmp_moneyelse:week_money.append(tmp_money)file_cnt += 1print "Your week money is",tmp_moneyall_money = all_money + tmp_moneyelse:passelse:breakf.close()print "\n","All used money is",all_moneyelse:continueprint "\n","eve_week_money:",week_money,"\n","total file:",file_cnt"""
将word文档转化为txt文档
"""
def word_to_txt():file_cnt = 0wordapp = win32com.client.gencache.EnsureDispatch("Word.Application")#输出路径print "Current path is "+PATH  #异常处理try:#遍历文件for root, dirs,files in os.walk(PATH):for _dir in dirs:passfor _file in files:#匹配doc文件if not fnmatch.fnmatch(_file,'*.doc'):#跳出循环语句continue#将目录和文件名拼接 例如:c:\smart\test.docword_file = os.path.join(root, _file)wordapp.Documents.Open(word_file)#修改成txt文件名  word_file[:-3]表示取从开始到倒数第三个字符docastxt = word_file[:-3] +'txt'#转换成txt文件wordapp.ActiveDocument.SaveAs(docastxt,FileFormat = win32com.client.constants.wdFormatText)wordapp.ActiveDocument.Close()file_cnt += 1print "file %dth convert success ."%file_cntfinally:wordapp.Quit()print "All file convert success !"if __name__ == '__main__':try:#word_to_txt()read_txt_data()week_to_month()money_plot()except IOError, e:print e

绘图如下:

分析后续加上。。。。。

python matplotlib阶段性总结——word转txt、绘图、文件操作相关推荐

  1. Python开发【第三篇】:文件操作与函数

    内容概要 文件操作 初始函数 函数定义与调用 函数的返回值 函数的参数 函数进阶 函数参数--动态传参 名称空间 作用域 函数的嵌套 函数名的运用 gloabal,nonlocal 关键字 1.文件操 ...

  2. python反转字符串(简单方法)及简单的文件操作示例

    Python反转字符串的最简单方法是用切片: >>> a='123456' >>> print a[::-1] 654321 切片介绍:切片操作符中的第一个数(冒号 ...

  3. python基础:python循环、三元运算、字典、文件操作

    目录: python循环 三元运算 字符串 字典 文件操作基础 一.python编程 在面向过程式编程语言的执行流程中包含: 顺序执行 选择执行 循环执行 if是条件判断语句:if的执行流程属于选择执 ...

  4. Python之路【第三篇】:文件操作

    一.文件操作步骤 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 歌名:<大火> 演唱:李佳薇 作词:姚若龙 作曲:马奕强 歌词: 有座巨大的停了的时钟 倾倒在赶 ...

  5. python学习笔记_第21天(文件操作--IO 技术)

    使用pickle 序列化 Python 中,一切皆对象,对象本质上就是一个"存储数据的内存块".有时候,我们需要将"内存块的数据"保存到硬盘上,或者通过网络传输 ...

  6. 【Python】Paramiko模块实现Linux服务器远程文件操作

    ssh是一个协议,OpenSSH是其中一个开源实现,paramiko是Python的一个库,实现了SSHv2协议(底层使用cryptography). 有了Paramiko以后,我们就可以在Pytho ...

  7. python实验项目_Python3实验 项目结构(文件操作)

    Python实验 项目结构(文件操作) 一.代码 # 姓名:池鱼奥 # 学号:201700000001 import os import time def make_project(project_n ...

  8. python另存为_python 将word另存为txt

    importosimportos.pathfrom win32com importclient as wc c=[] rootdir=["d:/77"] #以该路径为实验 deft ...

  9. python matplotlib 柱状图三个变量_Python 绘图,我只用 Matplotlib(三)—— 柱状图...

    Photo from Unsplash 上篇文章,我已经讲解绘制图像大致步骤,接下来的系列文章将分别对各种图形做讲解.其实就是了解各个图种的绘图 API.文章就讲解第一种图形,柱状图. 1 基础 绘制 ...

  10. Python机器学习数据预处理:读取txt数据文件并切分为训练和测试数据集

    背景信息 在使用Python进行机器学习时,经常需要自己完成数据的预处理,本节主要实现对txt文本数据的读取,该文本满足如下要求: 每行为一条样本数据,包括特征值与标签,标签在最后 样本数据的特征值之 ...

最新文章

  1. 山东人为什么爱用倒装句?没有吧我觉得。
  2. GTX 1080Ti + cuda8.0 + cuDNN6.0 安装及测试
  3. C++ :学习(类、指针)
  4. 排球积分程序(三)——模型类的设计
  5. 通过简易的前台代码实现无限二级域名转向(来自无忧 biyuan老矣)
  6. IntelliTrace 调试、定位异常
  7. 【spring】通过GZIP压缩提高网络传输效率(可以实现任何资源的gzip压缩、包括AJAX)
  8. 【BZOJ】2599: [IOI2011]Race 点分治
  9. 【转】关于维生素的那些事
  10. 华为鸿蒙系统多而能使用吗,【图片】华为鸿蒙系统的厉害之处在于 你可能非用不可 !【手机吧】_百度贴吧...
  11. 对setTimeout()第一个参数是字串的深入理解以及eval函数的理解
  12. 如何下载matlab,如何下载MATLAB?
  13. CentOS6.5下lv调整空间大小
  14. 未来5-10年计算机视觉发展趋势
  15. 【原创】小米路由器R1D 丢失SN号,刷回官方系统
  16. 30岁学前端晚不晚?别被年龄定义你的人生!
  17. Andriod-消息机制Handler
  18. 三维尺寸链计算和公差分析软件-DTAS-功能
  19. 智信分销拼团拍卖商城v3.38.3
  20. windows自带的比微信好用的截图工具:截取任意形状图片,标尺画直线,窗口图精准截取

热门文章

  1. 请教有关网络管理的方法
  2. 电磁波中的波段划分:L波段、S波段、C波段、X波段、Ku波段、K波段、Ka波段 等等
  3. Hinton的GLOM模型与千脑理论有何本质不同?
  4. 增量数据挖掘论文推荐
  5. 概率论与数理统计 第四版 浙江大学 盛骤,谢式千,潘承毅 个人阅读笔记
  6. 程序员利用Python定时抓取微博评论
  7. 前端性能优化的几种方案
  8. java怎么引入矢量图标库,阿里巴巴矢量图标库Iconfont的使用方法
  9. 政府大数据应用案例,政府大数据治理方法
  10. mysql 客户端命令行_强大的工具 MySQL客户端命令行应用技巧