I have a data file (trajectory file) which is not numerically sorted. The data file consists of texts and numbers repeatedly like the below. As you can see, the first 4 rows are just information, and the real numbers being sorted start with fifth row. Then again, another four rows are just information, then the number starts with the fifth row. Those are repeatedly hundred blocks. I would like to sort them numerically as the first column.
ITEM: TIMESTEP
0
ITEM: NUMBER OF ATOMS
ITEM: ATOMES id type x y z
4959 8 10.1 20.1 41.1
5029 8 13.1 43.1 5.3
....
ITEM: TIMESTEP
100
ITEM: NUMBER OF ATOMS
ITEM: ATOMES id type x y z
1259 8 10.1 20.1 41.1
6169 8 13.1 43.1 5.3
....
ITEM: TIMESTEP
200
ITEM: NUMBER OF ATOMS
ITEM: ATOMES id type x y z
3523 8 10.1 20.1 41.1
9119 8 13.1 43.1 5.3
....
I tried to make a python script. My idea is putting the each number block between 'ITEM: ATOMES id type x y z' and ITEM: NUMBER of ATOMS into list, then sort them in the list and print them. I have put them into list but the each element like (e.g., 4959 8 10.1 20.1 41.1) is just one string. How can I sort as the first column of the string in the list?
I tried as the following. Would you give me some advice?
f_in=open('aa', 'r')
def SORT(List):
print 'ITEM: TIMESTEP'
print 'Num of Trajectory'
print 'ITEM: NUMBER OF ATOMS'
print 'ATOMS'
print 'ITEM: BOX BOUNDS pp pp pp'
print '\n\n'
print 'ITEM: ATOMS id type x y z'
for p in List:
print p
LIST=[]
a = 1
for line in f_in:
sp = line.split()
if(len(sp) != 5):
continue
else:
if(a < 5085):
LIST.append(line)
a = a + 1
elif(a == 5085):
LIST.append(line)
LIST = map(lambda s: s.strip(), LIST)
SORT(LIST)
a = 1
splitthe line intospand check its size, you never usespagain; why?