You can use DataFrame
from module pandas
and read it as csv with separator |
import pandas as pd
df = pd.read_csv("data.txt", sep='|', dtype=str)
df = df.sort_values(' Setup Time - GPS (UTC) ')
df.to_csv('output.csv', sep='|', index=False)
Because read_csv()
tries to convert values to numbers so I use dtype=str
to keep all as strings.
I keeps all spaces in values and column's names so later I can write it back formated as before. But I have to also use spaces in name ' Setup Time - GPS (UTC) '
to sort it
Because DataFrame
adds index to every row so I have to skip indexes in to_csv()
EDIT: Example using io.StringIO
to read data from memory instead of file so everyone can easily test it without saving data in file.
import pandas as pd
import io
data ='''Equipment ID | Setup Time - GPS (UTC) | End Time - GPS (UTC)
3 | 2068512564500 (2019-08-30 22:22:26.500 UTC) | 2068513054300 (2019-08-30 22:30:36.300 UTC)
2 | 2068506579500 (2019-08-30 20:42:41.500 UTC) | 2068507041300 (2019-08-30 20:50:23.300 UTC)
2 | 2068513133500 (2019-08-30 22:31:55.500 UTC) | 2068513614300 (2019-08-30 22:39:56.300 UTC)
3 | 2068506038500 (2019-08-30 20:33:40.500 UTC) | 2068506399300 (2019-08-30 20:39:41.300 UTC)
1 | 2068512827500 (2019-08-30 22:26:49.500 UTC) | 2068512852300 (2019-08-30 22:27:14.300 UTC)'''
#file_ = "data.txt"
file_ = io.StringIO(data)
df = pd.read_csv(file_, sep='|', dtype=str)
df = df.sort_values(' Setup Time - GPS (UTC) ')
#df.to_csv('output.csv', sep='|', index=False)
pd.options.display.width = 150
pd.options.display.max_columns = 5
print(df)
Result in DataFrame
:
Equipment ID Setup Time - GPS (UTC) End Time - GPS (UTC)
3 3 2068506038500 (2019-08-30 20:33:40.500 UTC) 2068506399300 (2019-08-30 20:39:41.300 UTC)
1 2 2068506579500 (2019-08-30 20:42:41.500 UTC) 2068507041300 (2019-08-30 20:50:23.300 UTC)
0 3 2068512564500 (2019-08-30 22:22:26.500 UTC) 2068513054300 (2019-08-30 22:30:36.300 UTC)
4 1 2068512827500 (2019-08-30 22:26:49.500 UTC) 2068512852300 (2019-08-30 22:27:14.300 UTC)
2 2 2068513133500 (2019-08-30 22:31:55.500 UTC) 2068513614300 (2019-08-30 22:39:56.300 UTC)