Skip to content Skip to sidebar Skip to footer

Need Help To Sort Processed Mdb File In Python On Linux Machine

I am trying to extract a table from .mdb file, then filter that table and spit out the result into short .csv file. So far I was able to extract the table needed and save it's cont

Solution 1:

It may be easier not to deal with a flat list of rows but convert it to a stucture which would allow to "query" the data easier first. Something like a list of dicts, where each dict represents a cycle:

cycles = {}

rows = contents.splitlines()  # split the `contents` text blob into individual lines

for row in rows[1:]:  # the first line in your question is a header - [1:] skips it
    row = rows.split()  # split each line by whitespace
    cycle = cycles.setdefault(row[0], {'id': row[0], 'rows': []}
    cycle['rows'].append({'cycle':row[0], 'test_time': row[1], 'current': row[2], ...})

Then you can sort them by test_time:

for key, cycle in cycles.items():
    cycles['rows'].sort(key=itemgetter('test_time'))

Then you can process your data. The last row of each cycle:

 for key, cycle in cycles.items():
    output_row(cycles['rows'][-1])

Rows of the last five cycles:

 for key, cycle in sorted(cycles.items())[:-5]:
    output_rows(cycles['rows'])

Extract rows from 4 to 30:

for idx in range(4, 31):
    cycle = cycles[str(idx)]
    output_rows(cycles['rows'])

Post a Comment for "Need Help To Sort Processed Mdb File In Python On Linux Machine"