1

我需要按包含以下格式的 Temp5 列对 CSV 文件进行排序。在我的具体情况下,Temp5 列包含失败的值。
换句话说,它不包含任何值,只显示失败。

因此,我需要对 Temp5 中的值执行排序操作并忽略 Failed 值。

我可以编写新的 csv 文件或修改现有的文件。我已经csv在 python 和sortlinunx 中查看过命令但我找不到任何解决方案。因此new/Existing CSV File,在 Failed 值之后,我的 temp5 排序值(即没有丢失任何行和 Failed 值是任何顺序)

努力:我也尝试过使用python代码,它建议我将字典和存储列作为键(你想要排序)和值组成完整的行,而不是对键进行排序并根据键检索数据。但我遇到了问题,它不包括失败的值。请找到我写进python的函数

csv_s_mt0 = csv.reader(open("data.csv","rb"))
    s_mt0_map = {}
    s_mt1_map = {}
    line_escape = 0
    for line in csv_s_mt0:
        if(line_escape > 3):
            print line
            print line[4]
            s_mt0_map[line[4]] = line
        else:
            line_escape = line_escape + 1
    s_mt0_map_key = s_mt0_map.keys()
    s_mt0_map_key.sort()
    for key in s_mt0_map_key:
        print s_mt0_map_key[key]

    print len(s_mt0_map_key)


$Header Information
$Tool info=3
.TITLE '*****************************************************'
Temp1,Temp2,Temp3,Temp4,Temp5,Temp6,Temp6,Temp7,Temp8,Temp9
0.,   failed,   failed,-2.700e-10, 9.803e-11,-2.725e-11, 2.725e-11,-1.645e-06, -40.0000,1
1.000e-12,   failed,   failed,-2.689e-10, 9.805e-11,-2.731e-11, 2.731e-11, 6.571e-08, -40.0000,1
2.000e-12,   failed,   failed,-2.679e-10, 9.806e-11,-2.731e-11, 2.731e-11, 6.835e-08, -40.0000,1
3.000e-12,   failed,   failed,-2.669e-10, 9.805e-11,-2.729e-11, 2.729e-11, 1.376e-07, -40.0000,1
4.000e-12,   failed,   failed,-2.660e-10, 9.803e-11,-2.731e-11, 2.731e-11, 3.583e-08, -40.0000,1
5.000e-12,   failed,   failed,-2.649e-10, 9.807e-11,-2.725e-11, 2.725e-11,-1.646e-06, -40.0000,1
6.000e-12,   failed,   failed,-2.640e-10, 9.803e-11,-2.731e-11, 2.731e-11, 3.579e-08, -40.0000,1
7.000e-12,   failed,   failed,-2.630e-10, 9.801e-11,-2.728e-11, 2.728e-11, 1.828e-07, -40.0000,1
8.000e-12,   failed,   failed,-2.620e-10, 9.805e-11,-2.729e-11, 2.729e-11, 1.353e-07, -40.0000,1
4.940e-10,   failed,   failed, 2.241e-10,   failed,   failed,   failed,   0.8100, -40.0000,1
4.950e-10,   failed,   failed, 2.251e-10,   failed,   failed,   failed,   0.8100, -40.0000,1
4.960e-10,   failed,   failed, 2.261e-10,   failed,   failed,   failed,   0.8100, -40.0000,1
4.970e-10,   failed,   failed, 2.271e-10,   failed,   failed,   failed,   0.8100, -40.0000,1
4.980e-10,   failed,   failed, 2.280e-10,   failed,   failed,   failed,   0.8100, -40.0000,1
4.990e-10,   failed,   failed, 2.291e-10,   failed,   failed,   failed,   0.8100, -40.0000,1
5.000e-10,   failed,   failed, 2.301e-10,   failed,   failed,   failed,   0.8100, -40.0000,1
4

2 回答 2

1

key函数用于排序,它返回float('inf')所有的failed,所以它们都放在列表的底部。同样,float('-inf')如果您希望它们都在顶部,则可以使用。

>>> import csv
>>> import sys # to print to sys.stdout for this example
>>> from itertools import islice
>>> def key(n):
        return float(n) if n != 'failed' else float('inf')

>>> with open('data.csv') as f:
        info = list(islice(f, 0, 3)) # first 3 lines
        r = csv.DictReader(f)
        w = csv.DictWriter(sys.stdout, r.fieldnames)
        rows = sorted(r, key=lambda row: key(row['Temp5']))    
        sys.stdout.writelines(info)
        w.writeheader()
        w.writerows(rows)


$HeaderInformation
$Toolinfo=3
.TITLE'*****************************************************'
Temp1,Temp2,Temp3,Temp4,Temp5,Temp6,Temp6,Temp7,Temp8,Temp9    
7.000e-12,failed,failed,-2.630e-10,9.801e-11,2.728e-11,2.728e-11,1.828e-07,-40.0000,1    
0.,failed,failed,-2.700e-10,9.803e-11,2.725e-11,2.725e-11,-1.645e-06,-40.0000,1    
4.000e-12,failed,failed,-2.660e-10,9.803e-11,2.731e-11,2.731e-11,3.583e-08,-40.0000,1    
6.000e-12,failed,failed,-2.640e-10,9.803e-11,2.731e-11,2.731e-11,3.579e-08,-40.0000,1    
1.000e-12,failed,failed,-2.689e-10,9.805e-11,2.731e-11,2.731e-11,6.571e-08,-40.0000,1    
3.000e-12,failed,failed,-2.669e-10,9.805e-11,2.729e-11,2.729e-11,1.376e-07,-40.0000,1    
8.000e-12,failed,failed,-2.620e-10,9.805e-11,2.729e-11,2.729e-11,1.353e-07,-40.0000,1    
2.000e-12,failed,failed,-2.679e-10,9.806e-11,2.731e-11,2.731e-11,6.835e-08,-40.0000,1    
5.000e-12,failed,failed,-2.649e-10,9.807e-11,2.725e-11,2.725e-11,-1.646e-06,-40.0000,1    
4.940e-10,failed,failed,2.241e-10,failed,failed,failed,0.8100,-40.0000,1    
4.950e-10,failed,failed,2.251e-10,failed,failed,failed,0.8100,-40.0000,1    
4.960e-10,failed,failed,2.261e-10,failed,failed,failed,0.8100,-40.0000,1    
4.970e-10,failed,failed,2.271e-10,failed,failed,failed,0.8100,-40.0000,1    
4.980e-10,failed,failed,2.280e-10,failed,failed,failed,0.8100,-40.0000,1    
4.990e-10,failed,failed,2.291e-10,failed,failed,failed,0.8100,-40.0000,1    
5.000e-10,failed,failed,2.301e-10,failed,failed,failed,0.8100,-40.0000,1
于 2013-04-24T06:34:21.377 回答
0

Try this:

>>> c = list(csv.reader(s)) # here s is the file with the headers skipped
>>> final = []
>>> for row in c:
...    row = map(lambda x: x.strip(), row)
...    Temp5 = row[4]
...    if Temp5 != 'failed':
...        final.append(row)
>>> myprint.tabular(final) # just a pretty-printing function...
+ --------- + ------ + ------ + ---------- + --------- + ---------- + --------- + ---------- + -------- + ----- +
|   Temp1   | Temp2  | Temp3  |   Temp4    |   Temp5   |   Temp6    |   Temp6   |   Temp7    |  Temp8   | Temp9 |
+ --------- + ------ + ------ + ---------- + --------- + ---------- + --------- + ---------- + -------- + ----- +
|     0.    | failed | failed | -2.700e-10 | 9.803e-11 | -2.725e-11 | 2.725e-11 | -1.645e-06 | -40.0000 |   1   |
| 1.000e-12 | failed | failed | -2.689e-10 | 9.805e-11 | -2.731e-11 | 2.731e-11 | 6.571e-08  | -40.0000 |   1   |
| 2.000e-12 | failed | failed | -2.679e-10 | 9.806e-11 | -2.731e-11 | 2.731e-11 | 6.835e-08  | -40.0000 |   1   |
| 3.000e-12 | failed | failed | -2.669e-10 | 9.805e-11 | -2.729e-11 | 2.729e-11 | 1.376e-07  | -40.0000 |   1   |
| 4.000e-12 | failed | failed | -2.660e-10 | 9.803e-11 | -2.731e-11 | 2.731e-11 | 3.583e-08  | -40.0000 |   1   |
| 5.000e-12 | failed | failed | -2.649e-10 | 9.807e-11 | -2.725e-11 | 2.725e-11 | -1.646e-06 | -40.0000 |   1   |
| 6.000e-12 | failed | failed | -2.640e-10 | 9.803e-11 | -2.731e-11 | 2.731e-11 | 3.579e-08  | -40.0000 |   1   |
| 7.000e-12 | failed | failed | -2.630e-10 | 9.801e-11 | -2.728e-11 | 2.728e-11 | 1.828e-07  | -40.0000 |   1   |
| 8.000e-12 | failed | failed | -2.620e-10 | 9.805e-11 | -2.729e-11 | 2.729e-11 | 1.353e-07  | -40.0000 |   1   |
+ --------- + ------ + ------ + ---------- + --------- + ---------- + --------- + ---------- + -------- + ----- +
于 2013-04-24T06:42:42.677 回答