gpt4 book ai didi

python - 如何在 Linux 上的 Python 中对该表进行排序

转载 作者:太空宇宙 更新时间:2023-11-04 05:38:46 24 4
gpt4 key购买 nike

import sys, subprocess, glob

mdbfiles = glob.glob('*.res')
for DATABASE in mdbfiles:

subprocess.call(["mdb-schema", DATABASE, "mysql"])

table_names = subprocess.Popen(["mdb-tables", "-1", DATABASE],
stdout=subprocess.PIPE).communicate()[0]
tables = table_names.splitlines()

sys.stdout.flush()

a=str('Channel_Normal_Table')

for table in tables:
if table != '' and table==a:

filename = DATABASE.replace(".res","") + ".csv"
file = open(filename, 'w')
print("Dumping " + table)
contents = subprocess.Popen(["mdb-export", DATABASE, table],
stdout=subprocess.PIPE).communicate()[0]

# I NEED TO PUT SOMETHING HERE TO SORT AND EXTRACT THE DATA I NEED


file.write(contents)
file.close()

我有一个从数据库中提取的表。我们称之为table 。我需要执行以下操作,但有点卡住了:

Cycle Test_Time  Current    Voltage
1 7.80E-002 0.00E+000 1.21E-001
1 3.01E+001 0.00E+000 1.19E-001
1 6.02E+001 0.00E+000 1.17E-001
2 9.02E+001 0.00E+000 1.14E-001
2 1.20E+002 0.00E+000 1.11E-001
2 1.50E+002 0.00E+000 1.08E-001
2 1.80E+002 0.00E+000 1.05E-001
2 2.10E+002 0.00E+000 1.02E-001
3 2.40E+002 0.00E+000 9.93E-002
3 2.70E+002 0.00E+000 9.66E-002
3 3.00E+002 0.00E+000 9.38E-002
3 3.10E+002 4.00E-001 1.26E+000
  1. 提取每个周期的最后(最新)行,或者更高级的是,对周期进行排序按时间并提取具有最新时间的周期的行。作为你可以看到,最后一行并不总是最新的时间,因为我们的测试机会出现故障,但通常都会出现。但越大时间越晚编号。
  2. 提取最近五个周期的所有行
  3. 提取从周期 4 到周期 30 的所有行。

我尝试了各种方法,例如根据我有限的 Python 知识创建和排序字典和列表,但没有一个得到所需的输出。这简直让我发疯。非常感谢!

最佳答案

这并不难,但你必须一步一步来:

from collections import defaultdict

table = """\
Cycle Test_Time Current Voltage
1 7.80E-002 0.00E+000 1.21E-001
1 3.01E+001 0.00E+000 1.19E-001
1 6.02E+001 0.00E+000 1.17E-001
2 9.02E+001 0.00E+000 1.14E-001
2 1.20E+002 0.00E+000 1.11E-001
2 1.50E+002 0.00E+000 1.08E-001
2 1.80E+002 0.00E+000 1.05E-001
2 2.10E+002 0.00E+000 1.02E-001
3 2.40E+002 0.00E+000 9.93E-002
3 2.70E+002 0.00E+000 9.66E-002
3 3.00E+002 0.00E+000 9.38E-002
3 3.10E+002 4.00E-001 1.26E+000"""

# Split into rows
table = table.splitlines()

# Split each row into values
table = [row.split() for row in table]

# Associate the column names with their index
headers = table.pop(0)
H = {x: i for i, x in enumerate(headers)}
time_index = H["Test_Time"]
cycle_index = H["Cycle"]

# Sort by Test_Time
table.sort(key=lambda row: float(row[time_index]))

# Associate each test with its cycle
D = defaultdict(list)
for row in table:
D[int(row[cycle_index])].append(row)

# Present the information
print(*headers, sep='\t')
print("Latest row for each cycle")
for cycle in sorted(D.keys()):
tests = D[cycle]
latest_test = tests[-1]
print(*latest_test, sep='\t')

print("All rows for last 5 cycles")
for cycle in sorted(D.keys())[-5:]:
tests = D[cycle]
for test in tests:
print(*test, sep='\t')

print("All rows for cycles 4 through 30")
for cycle in sorted(D.keys()):
if 4 <= cycle <= 30:
tests = D[cycle]
for test in tests:
print(*test, sep='\t')

关于python - 如何在 Linux 上的 Python 中对该表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17173995/

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