can anyone help please?
i have a snip of my file below can the the numbers be sorted as in excel?


27 4 220 16 0.76151102 1.06059456
27 4 220 17 0.61465055 0.90107796
27 4 221 22 0.81186094 1.12214581
27 4 223 28 0.69887889 0.98993400
27 4 225 30 0.74796748 1.04485376

i dont even know were to start argggggggghhhh please help

Dani AI

Generated

A concise, practical plan to get those whitespace-separated rows sorted like Excel. is right — it can be done. already showed the basic idea (turn each line into a list of values, then sort by the column you want). Below are safer, more flexible notes and a small, different example you can drop into a script.

Read and parse

  • Split each non-empty line on whitespace (use line.split() or re.split(r'\s+', ...) to be explicit).
  • Convert tokens safely: prefer int() then fall back to float() (or always float() if you only care about numeric ordering). Do not use eval() on file data — it is a security risk.

Sort and write

  • Use the stable built-in sort with a key. The index is zero-based: column 1 is index 0.
  • For a non-destructive approach use sorted(...); to change the list in place use list.sort(...).
  • To write the output keep control of formatting (e.g., format(x, '.8f') for floats) so decimal precision matches what you expect.

Small example (parsing, sorting by column index 4, writing back):

from operator import itemgetter

# populate rows with safe numeric conversion as strings -> ints/floats
# rows = [ [27, 4, 220, 16, 0.76151102, 1.06059456], ... ]

rows.sort(key=itemgetter(4))   # zero-based index for the column you want

with open('sorted.txt', 'w') as out:
    for r in rows:
        out.write(' '.join(map(str, r)) + '\n')

Troubleshooting and tips

  • Multi-column sorts: use key=itemgetter(a,b) or key=lambda r: (r[a], r[b]).
  • Very large files: use external Unix sort or tools like pandas.read_csv(..., sep=r'\s+') / numpy.loadtxt rather than loading everything into Python lists.
  • Preserve original formatting by storing the original line alongside parsed values if you need to keep exact spacing or trailing zeros.

This addresses the OP approach and improves on by avoiding eval() and showing more general, production-friendly guidance.

Recommended Answers

All 3 Replies

Yes, they can.

commented: haha! :) +4

any suggestions please

You have to convert your data string to list of lists and then sort the sublists by item index:

s = """\
27 4 220 16 0.76151102 1.06059456
27 4 220 17 0.61465055 0.90107796
27 4 221 22 0.81186094 1.12214581
27 4 223 28 0.69887889 0.98993400
27 4 225 30 0.74796748 1.04485376"""

# create list of lists from the data string
mylist = []
for line in s.splitlines():
    temp_list = []
    for item in line.split():
        temp_list.append(eval(item))
    mylist.append(temp_list)

print("Original lists:")
for sublist in mylist:
    print(sublist)

print('-'*40)

print("Sorted by the 5th item:")
# inplace sort by the 5th item which is at index 4
ix = 4
mylist.sort(key=lambda x: x[ix])
for sublist in mylist:
    print(sublist)

"""result=
Original lists:
[27, 4, 220, 16, 0.76151102, 1.06059456]
[27, 4, 220, 17, 0.61465055, 0.90107796]
[27, 4, 221, 22, 0.81186094, 1.12214581]
[27, 4, 223, 28, 0.69887889, 0.989934]
[27, 4, 225, 30, 0.74796748, 1.04485376]
----------------------------------------
Sorted by the 5th item:
[27, 4, 220, 17, 0.61465055, 0.90107796]
[27, 4, 223, 28, 0.69887889, 0.989934]
[27, 4, 225, 30, 0.74796748, 1.04485376]
[27, 4, 220, 16, 0.76151102, 1.06059456]
[27, 4, 221, 22, 0.81186094, 1.12214581]
"""
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.