#Python 实现得到现在时间12个月前的每个月

# 假设现在的时间是2016年9月25日

#得到现在的时间  得到now等于2016年9月25日
now = datetime.datetime.now()
#得到今年的的时间 (年份) 得到的today_year等于2016年
today_year = now.year
#今年的时间减去1,得到去年的时间。last_year等于2015
last_year = int(now.year) -1
#得到今年的每个月的时间。today_year_months等于1 2 3 4 5 6 7 8 9,
today_year_months = range(1,now.month+1)
#得到去年的每个月的时间 last_year_months 等于10 11 12
last_year_months = range(now.month+1, 13)
#定义列表去年的数据
data_list_lasts = []
#通过for循环,得到去年的时间夹月份的列表
#先遍历去年每个月的列表
for last_year_month in last_year_months:
# 定义date_list 去年加上去年的每个月
date_list = '%s-%s' % (last_year, last_year_month)
#通过函数append,得到去年的列表
data_list_lasts.append(date_list) data_list_todays = []
#通过for循环,得到今年的时间夹月份的列表
#先遍历今年每个月的列表
for today_year_month in today_year_months:
# 定义date_list 去年加上今年的每个月
data_list = '%s-%s' % (today_year, today_year_month)
#通过函数append,得到今年的列表
data_list_todays.append(data_list)
#去年的时间数据加上今年的时间数据得到年月时间列表
data_year_month = data_list_lasts + data_list_todays
data_year_month.reverse()

程序运行过程图片

Python得到前面12个月的数据,Python得到现在时间 前一年的数据,的更多相关文章

  1. Python高级特性: 12步轻松搞定Python装饰器

    12步轻松搞定Python装饰器 通过 Python 装饰器实现DRY(不重复代码)原则:  http://python.jobbole.com/84151/   基本上一开始很难搞定python的装 ...

  2. QlikView显示所选时间前一年的数据

    客户常常提出这种需求,当用户选择某一时间时.图表中显示所选时间之前一年的数据.以下是我的方法.如有不当,请多不吝赐教: 数据准备例如以下所看到的: SalesData: LOAD Num(ID) as ...

  3. Postgresql查询最近12个月、最近30天数据

    -- 最近 12 个月 SELECT * FROM 表名 WHERE 日期字段 BETWEEN (now() - INTERVAL '12 months') AND now() -- 最近 30 天 ...

  4. 小白学 Python 数据分析(12):Pandas (十一)数据透视表(pivot_table)

    人生苦短,我用 Python 前文传送门: 小白学 Python 数据分析(1):数据分析基础 小白学 Python 数据分析(2):Pandas (一)概述 小白学 Python 数据分析(3):P ...

  5. python 全栈开发,Day43(python全栈11期月考题)

    python全栈11期月考题 1.常用字符串格式化有哪些?并说明他们的区别 2.请手写一个单例模式(面试题) 3.利用 python 打印前一天的本地时间,格式为‘2018-01-30’(面试题) 4 ...

  6. 6月5日 python复习 模块

    """1. os和sys都是干什么的?2. 你工作中都用过哪些内置模块?3. 有没有用过functools模块?"""1. os 系统相关 ...

  7. 4月8日 python学习总结 模块与包

    一.包 #官网解释 Packages are a way of structuring Python's module namespace by using "dotted module n ...

  8. 5月14日 python学习总结 视图、触发器、事务、存储过程、函数、流程控制、索引

    一.视图 1.什么是视图 视图就是通过查询得到一张虚拟表,然后保存下来,下次用的直接使用即可 2.为什么要用视图 如果要频繁使用一张虚拟表,可以不用重复查询 3.如何用视图 create view t ...

  9. python爬虫成长之路(一):抓取证券之星的股票数据

    获取数据是数据分析中必不可少的一部分,而网络爬虫是是获取数据的一个重要渠道之一.鉴于此,我拾起了Python这把利器,开启了网络爬虫之路. 本篇使用的版本为python3.5,意在抓取证券之星上当天所 ...

随机推荐

  1. WKWebView 与 UIWebView

    apple和google为webkit该浏览器引擎的发扬光大做出了重要贡献,在WWDC 2014发布会上发布iOS 8中,apple公布了WebKit框架,这意味着OSX和IOS开发者将共用同样的开发 ...

  2. [工作积累] Android system dialog with native callback

    JNI: invoke java dialog with native callback: store native function address in java, and invoke nati ...

  3. tomcat 一个服务 多端口网站

    多站点多端口   <Service name="Catalina">      <Connector port="8080" protocol ...

  4. Itunes connect State: Developer Action Needed

    In-App Purchases have been returned and are highlighted in the table below. Your In-App Purchase has ...

  5. Sqli-labs less 53

    Less-53 和less51是一样的,只是这里的mysql错误不会在前台显示,但是对于stacked injection是一样的利用方式 http://127.0.0.1/sqli-labs/Les ...

  6. 3[doses] ------一种诡异的写法

    在 head first c 的第60页,有这么一道题: 一个富翁因为服药过度而死亡. 下面是自动服药器的代码: #include <stdio.h> int main(void) { , ...

  7. LA 2038

    Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solu ...

  8. DllImport 相关错误

    问题: 当我用 [DllImport("*.dll", EntryPoint = "*",CallingConvention = CallingConventi ...

  9. C# 设置程序开机自动运行(+注册表项)

    有时候我们需要让软件安装好了,开机自动运行,这时我们需要把启动项加载到注册表中,需要注意的时现在很多杀毒软件在其他软件更改注册表的时候会有提示,可能会阻止.下面代码包含增加启动项到注册表和删除启动项. ...

  10. HDU5568/BestCoder Round #63 (div.2) B.sequence2 dp+高精度

    sequence2 Problem Description Given an integer array bi with a length of n, please tell me how many ...