Home > database >  How to Sort .txt File Items in a List
How to Sort .txt File Items in a List

Time:07-11

I have a set of coordinates for 2 points (0 and 1) in list data:

data: [0 [834.5, 211.5]][1 [941.5, 362.5]]\n
[0 [834.5, 213.5]][1 [943.0, 362.5]]\n
[0 [825.0, 206.0]][1 [961.5, 360.0]]\n
[0 [814.0, 201.0]][1 [973.5, 358.5]]\n
[0 [813.0, 200.5]][1 [973.5, 357.5]]\n
[0 [779.0, 184.0]][1 [992.5, 358.0]]\n
[0 [758.0, 174.0]][1 [993.5, 366.0]]\n
[0 [758.0, 173.5]][1 [992.5, 366.5]]\n
[0 [714.5, 160.0]][1 [991.0, 391.0]]\n
[0 [691.0, 154.5]][1 [1012.0, 372.0]]\n
[0 [690.5, 154.0]][1 [1012.0, 372.0]]\n
[0 [648.0, 157.5]][1 [1012.0, 372.0]]\n
[0 [631.5, 164.0]][1 [1012.0, 372.0]]\n

I need to put the column starting with 834.5 in head_x list, 211.5 in head_y list, 941.5 in foot_x, and 362.5 in foot_y. For the first 9 rows, I used this code:

for i in range(4, len(data), 37):
    head_x.append(data[i:i 5])

for i in range(11, len(data), 37):
    head_y.append(data[i: i 5])

for i in range(22, len(data), 37):
    foot_x.append(data[i: i 5])

for i in range(29, len(data), 37):
    foot_y.append(data[i: i 5])

However, I am not sure how to deal with numbers like 1012.0. I have tried to write some code that would change the for-loop if the number > 999, but each character is an element in the overall large list, so that does not work. I am not really sure how to approach this, so some help would be greatly appreciated.

This was the unfortunate output for head_x:

['834.5', '834.5', '825.0', '814.0', '813.0', '779.0', '758.0', '758.0', '714.5', '691.0', '[690.', ' [648', '0 [63', '[0 [6', '\n[0 [', ']\n[0 ', ']]\n[0', '0]]\n[', '0]]\n[', '0]]\n[', '0]]\n[', '0]]\n[', '0]]\n[', '0]]\n[', '0]]\n[', '0]]\n[', '0]]']

CodePudding user response:

Try a different, but similar approach, by using the str.find method.

Here I go line by line, find the start/end indexes of each number, and then slice the line string with these. The second argument of str.find is the index at which it should start finding the substring, so it progressively moves along as it finds each number.

data = r"""[0 [834.5, 211.5]][1 [941.5, 362.5]]\n
[0 [834.5, 213.5]][1 [943.0, 362.5]]\n
[0 [825.0, 206.0]][1 [961.5, 360.0]]\n
[0 [814.0, 201.0]][1 [973.5, 358.5]]\n
[0 [813.0, 200.5]][1 [973.5, 357.5]]\n
[0 [779.0, 184.0]][1 [992.5, 358.0]]\n
[0 [758.0, 174.0]][1 [993.5, 366.0]]\n
[0 [758.0, 173.5]][1 [992.5, 366.5]]\n
[0 [714.5, 160.0]][1 [991.0, 391.0]]\n
[0 [691.0, 154.5]][1 [1012.0, 372.0]]\n
[0 [690.5, 154.0]][1 [1012.0, 372.0]]\n
[0 [648.0, 157.5]][1 [1012.0, 372.0]]\n
[0 [631.5, 164.0]][1 [1012.0, 372.0]]\n"""

head_x = []
head_y = []
foot_x = []
foot_y = []

for line in data.splitlines():
    left = 4
    right = line.find(",", left)
    head_x.append(line[left:right])
    
    left = right   2
    right = line.find("]", left)
    head_y.append(line[left:right])
    
    left = line.find(" [", right)   2
    right = line.find(",", left)
    foot_x.append(line[left:right])
    
    left = right   2
    right = line.find("]", left)
    foot_y.append(line[left:right])

print(head_x)

The fortunate output for head_x:

['834.5', '834.5', '825.0', '814.0', '813.0', '779.0', '758.0', '758.0', '714.5', '691.0', '690.5', '648.0', '631.5']
  • Related