gpt4 book ai didi

python - 排序 CSV 文件 Python/Linux 命令

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:03:56 25 4
gpt4 key购买 nike

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

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

我可以编写新的 csv 文件或修改现有文件。我查看了 python 中的 csv 和 linunx 中的 sort 命令,但我找不到任何解决方案。所以在 new/Existing CSV File 中,我的 temp5 排序值比 Failed 值(即没有丢失任何行和 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

最佳答案

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

关于python - 排序 CSV 文件 Python/Linux 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16184750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com