【发布时间】:2011-02-14 04:19:32
【问题描述】:
如何列出数字 01 到 12(12 个月中的每个数字),以便当前月份总是排在最后,而最旧的月份排在最后。换句话说,如果该数字大于当前月份,则为上一年。
例如02 是 2011 年 2 月(当前月份),03 是 2010 年 3 月,09 是 2010 年 9 月,但 01 是 2011 年 1 月。在这种情况下,我想要 @ 987654327@。这就是我要确定年份的方法:
for inFile in os.listdir('.'):
if inFile.isdigit():
month = months[int(inFile)]
if int(inFile) <= int(strftime("%m")):
year = strftime("%Y")
else:
year = int(strftime("%Y"))-1
mnYear = month + ", " + str(year)
我不知道下一步该做什么。我应该在这里做什么?
更新:
我认为,我最好上传整个脚本以便更好地理解。
#!/usr/bin/env python
import os, sys
from time import strftime
from calendar import month_abbr
vGroup = {}
vo = "group_lhcb"
SI00_fig = float(2.478)
months = tuple(month_abbr)
print "\n%-12s\t%10s\t%8s\t%10s" % ('VOs','CPU-time','CPU-time','kSI2K-hrs')
print "%-12s\t%10s\t%8s\t%10s" % ('','(in Sec)','(in Hrs)','(*2.478)')
print "=" * 58
for inFile in os.listdir('.'):
if inFile.isdigit():
readFile = open(inFile, 'r')
lines = readFile.readlines()
readFile.close()
month = months[int(inFile)]
if int(inFile) <= int(strftime("%m")):
year = strftime("%Y")
else:
year = int(strftime("%Y"))-1
mnYear = month + ", " + str(year)
for line in lines[2:]:
if line.find(vo)==0:
g, i = line.split()
s = vGroup.get(g, 0)
vGroup[g] = s + int(i)
sumHrs = ((vGroup[g]/60)/60)
sumSi2k = sumHrs*SI00_fig
print "%-12s\t%10s\t%8s\t%10.2f" % (mnYear,vGroup[g],sumHrs,sumSi2k)
del vGroup[g]
当我运行脚本时,我得到了这个:
[root@serv07 usage]# ./test.py
VOs CPU-time CPU-time kSI2K-hrs
(in Sec) (in Hrs) (*2.478)
==================================================
Jan, 2011 211201372 58667 145376.83
Dec, 2010 5064337 1406 3484.07
Feb, 2011 17506049 4862 12048.04
Sep, 2010 210874275 58576 145151.33
正如我在原帖中所说,我希望结果是这样的:
Sep, 2010 210874275 58576 145151.33
Dec, 2010 5064337 1406 3484.07
Jan, 2011 211201372 58667 145376.83
Feb, 2011 17506049 4862 12048.04
源目录中的文件如下所示:
[root@serv07 usage]# ls -l
total 3632
-rw-r--r-- 1 root root 1144972 Feb 9 19:23 01
-rw-r--r-- 1 root root 556630 Feb 13 09:11 02
-rw-r--r-- 1 root root 443782 Feb 11 17:23 02.bak
-rw-r--r-- 1 root root 1144556 Feb 14 09:30 09
-rw-r--r-- 1 root root 370822 Feb 9 19:24 12
我现在有更好的图片吗?抱歉,一开始不是很清楚。干杯!!
更新@Mark Ransom
这是马克建议的结果:
[root@serv07 usage]# ./test.py
VOs CPU-time CPU-time kSI2K-hrs
(in Sec) (in Hrs) (*2.478)
==========================================================
Dec, 2010 5064337 1406 3484.07
Sep, 2010 210874275 58576 145151.33
Feb, 2011 17506049 4862 12048.04
Jan, 2011 211201372 58667 145376.83
正如我之前所说,我正在寻找按以下顺序打印的结果:2010 年 9 月 -> 2010 年 12 月 -> 2011 年 1 月 -> 2011 年 2 月 干杯!!
【问题讨论】:
-
从这里看来您的文件是按月份命名的?
-
根据您的描述,所需的输出不是 [03, 09, 01, 02] 而不是 [09, 03, 01, 02] 吗?
-
@geekGod:你是对的 - 它应该是 [03, 09, 01, 02]。这是我的错。
-
@KyleWpppd:是的,文件以月份命名。这些是一年中每月的 CPU 使用率,我正在尝试按月生成报告。
标签: python