【发布时间】:2013-07-07 14:44:45
【问题描述】:
我正在尝试根据需要连续发生的某些条件对表格进行排序。 表格的简化版本:
Number Time
1 23
2 45
3 67
4 23
5 11
6 45
7 123
8 34
...
我需要检查时间是否连续 5 次
这是我的代码的一部分:
reader = csv.reader(open(filename))
counter, temp1, temp2, numrow = 0, 0, 0, 0
for row in reader:
numrow+=1
if numrow <5:
col0, col1, col4, col5, col6, col23, col24, col25 = float(row[0]),
float(row[1]), float(row[4]), float(row[5]),float(row[6]),
float(row[23]), float(row[24]), float(row[25])
if col1 <= 40:
list1=(col1, col3, col4, col5, col6, col23, col24, col25)
counter += 1
if counter == 3:
print("Cell# %s" %filename[-10:-5])
print LAYOUT.format(*headers_short)
print LAYOUT.format(*temp1)
print LAYOUT.format(*temp2)
print LAYOUT.format(*list1)
print ""
elif counter == 1:
temp1=list1
elif counter == 2:
temp2=list1
else:
counter = 0
我实施了 Bakuriu 建议的解决方案,它似乎正在工作。但是,结合众多测试的最佳方式是什么?就像我需要检查几个条件一样。让我们说: v
- 连续 10 次循环效率低于 40,
- 连续 5 个循环中小于 40 个的容量
- 连续 25 个周期少于 40 次
- 还有其他一些...
现在我只为每次测试打开 csv.reader 并运行该函数。我想这不是最有效的方法,尽管它有效。对不起,我只是个菜鸟。
csvfiles = glob.glob('processed_data/*.stat')
for filename in csvfiles:
flag=[]
flag.append(filename[-12:-5])
reader = csv.reader(open(filename))
for a, row_group in enumerate(row_grouper(reader,10)):
if all(float(row[1]) < 40 for row in row_group):
str1= "Efficiency is less than 40 in cycles "+ str(a+1)+'-'+str(a+10) #i is the index of the first row in the group.
flag.append(str1)
break #stop processing other rows.
reader = csv.reader(open(filename))
for b, row_group in enumerate(row_grouper(reader,5)):
if all(float(row[3]) < 40 for row in row_group):
str1= "Capacity is less than 40 minutes in cycles "+ str(a+1)+'-'+str(a+5)
flag.append(str1)
break #stop processing other rows.
reader = csv.reader(open(filename))
for b, row_group in enumerate(row_grouper(reader,25)):
if all(float(row[3]) < 40 for row in row_group):
str1= "Time is less than < 40 in cycles "+ str(a+1)+'-'+str(a+25)
flag.append(str1)
break #stop processing other rows.
if len(flag)>1:
for i in flag:
print i
print '\n'
【问题讨论】:
-
这是一个非常规的“排序”含义...
标签: python csv conditional