[Solved] Sorting multiple rows as a single column using Python.


import re
import csv

with open('data.txt') as f:
    data = []
    for line in f:
        items = re.split('[\s]+', line.strip())
        if not line.strip().startswith('K1'):
            data.append([])
            data[-1].extend([items[0]] + items[2:])

        else:
            data[-1].extend(items[1:])

data = list(map(list, zip(*data)))

with open('data.csv', 'w') as f:   
    writer = csv.writer(f)
    writer.writerows(data)

And your csv data looks like this:

-29-,-30-,-31-,-32-,-33-,-34-,-35-,-36-,-37-
4.2735E+05,9.2186E+05,9.4197E+05,9.4089E+05,9.4889E+05,9.5109E+05,9.6455E+05,9.4382E+05,-4.8051E+06
4.3904E+05,9.2199E+05,9.4200E+05,9.4070E+05,9.4904E+05,9.5097E+05,9.6459E+05,9.4314E+05,-2.1630E+07
4.5718E+05,9.2134E+05,9.4127E+05,9.4016E+05,9.4849E+05,9.5042E+05,9.6377E+05,9.4290E+05,-8.8415E+07
4.8817E+05,9.2164E+05,9.4106E+05,9.4026E+05,9.4857E+05,9.5052E+05,9.6384E+05,9.4317E+05,-2.3794E+08
5.4312E+05,9.2075E+05,9.4132E+05,9.4015E+05,9.4842E+05,9.5047E+05,9.6355E+05,9.4365E+05,-5.2223E+08
7.3055E+05,9.1836E+05,9.4327E+05,9.3932E+05,9.4797E+05,9.4989E+05,9.6521E+05,9.4070E+05,-1.7084E+09
1.1069E+06,9.1954E+05,9.4194E+05,9.3955E+05,9.4799E+05,9.5001E+05,9.6406E+05,9.4213E+05,-4.2516E+09
1.6420E+06,9.1928E+05,9.4225E+05,9.3942E+05,9.4809E+05,9.4991E+05,9.6479E+05,9.4145E+05,-8.0371E+09
2.4611E+06,3.0813E+06,3.0970E+06,3.0909E+06,3.0905E+06,3.0875E+06,3.0927E+06,3.0611E+06,-1.4024E+10
3.4179E+06,3.9755E+06,3.9942E+06,4.0011E+06,4.0104E+06,4.0237E+06,4.0407E+06,4.0363E+06,-2.1404E+10
4.4951E+06,2.3131E+06,4.8227E+06,2.3339E+06,4.8430E+06,2.3439E+06,4.8796E+06,2.3368E+06,-3.0006E+10
5.6975E+06,2.5546E+06,5.5341E+06,2.5748E+06,5.5598E+06,2.5840E+06,5.6050E+06,2.5769E+06,-3.9706E+10
6.9872E+06,2.7332E+06,6.1198E+06,2.7522E+06,6.1519E+06,2.7604E+06,6.2076E+06,2.7535E+06,-5.0364E+10
8.3390E+06,2.8727E+06,6.6016E+06,2.8903E+06,6.6411E+06,2.8977E+06,6.7089E+06,2.8914E+06,-6.1872E+10
9.7442E+06,2.9952E+06,7.0045E+06,3.0113E+06,7.0524E+06,3.0186E+06,7.1343E+06,3.0145E+06,-7.4177E+10
1.1191E+07,3.0875E+06,7.3187E+06,3.1016E+06,7.3755E+06,3.1094E+06,7.4727E+06,3.1088E+06,-8.7199E+10
1.2660E+07,3.1166E+06,7.5190E+06,3.1288E+06,7.5859E+06,3.1385E+06,7.7009E+06,3.1442E+06,-1.0083E+11
1.4145E+07,3.1092E+06,7.6389E+06,3.1199E+06,7.7166E+06,3.1320E+06,7.8500E+06,3.1438E+06,-1.1505E+11
1.5650E+07,3.0592E+06,7.6793E+06,3.0689E+06,7.7689E+06,3.0848E+06,7.9231E+06,3.1050E+06,-1.2990E+11
1.7172E+07,2.9703E+06,7.6466E+06,2.9790E+06,7.7489E+06,3.0000E+06,7.9256E+06,3.0301E+06,-1.4541E+11
1.8700E+07,2.8246E+06,7.5266E+06,2.8324E+06,7.6431E+06,2.8600E+06,7.8451E+06,2.9032E+06,-1.6152E+11
2.0219E+07,2.6487E+06,7.3461E+06,2.6560E+06,7.4778E+06,2.6914E+06,7.7070E+06,2.7489E+06,-1.7815E+11
2.1725E+07,2.4648E+06,7.1328E+06,2.4711E+06,7.2790E+06,2.5138E+06,7.5352E+06,2.5853E+06,-1.9528E+11
2.3218E+07,2.2807E+06,6.8899E+06,2.2859E+06,7.0506E+06,2.3357E+06,7.3340E+06,2.4205E+06,-2.1295E+11
2.4696E+07,2.0752E+06,6.6004E+06,2.0793E+06,6.7754E+06,2.1366E+06,7.0864E+06,2.2355E+06,-2.3114E+11
2.6156E+07,1.7324E+06,6.2507E+06,1.7387E+06,6.4401E+06,1.8181E+06,6.7807E+06,1.9550E+06,-2.4986E+11
2.7587E+07,1.3371E+06,5.8471E+06,1.3434E+06,6.0493E+06,1.4438E+06,6.4190E+06,1.6183E+06,-2.6904E+11
2.8989E+07,8.6208E+05,5.3625E+06,8.6692E+05,5.5774E+06,9.8956E+05,5.9785E+06,1.2061E+06,-2.8870E+11
3.0361E+07,3.1255E+05,4.8011E+06,3.1210E+05,5.0257E+06,4.5490E+05,5.4571E+06,7.1347E+05,-3.0891E+11
3.1718E+07,4.3936E+04,4.1676E+06,4.0843E+04,4.3994E+06,1.9655E+05,4.8602E+06,4.8245E+05,-3.2966E+11
3.3164E+07,-3.2260E+05,3.4331E+06,-3.2145E+05,3.6721E+06,-1.5399E+05,4.1565E+06,1.5065E+05,-3.5090E+11
3.4862E+07,-6.0556E+05,2.9632E+06,-6.2328E+05,3.1921E+06,-4.5865E+05,3.6840E+06,-1.4123E+05,-3.7072E+11

1

solved Sorting multiple rows as a single column using Python.