I need to have a file sorted with consistent way as Python would do so.
I have some file sorted using Unix sort
program. After sorting this file, I wrote Python script for checking if that was sorted correctly:
with open('my_file_location') as f:
last_l = next(f)
for l in f:
if last_l > l:
print(last_l, l)
break
last_l = l
Script failed giving the following entry:
('250,8\n', '25,1\n')
I experimented a bit with sort tool, to check if the output is actually repeatable and inconsistent with Python comparison algorithm. Finally, I found two interesting cases:
$ echo -e "250,1\n25,8" | sort
250,1
25,8
$ echo -e "250,\n25," | sort
25,
250,
Why these two calls give me two different orders? I consider it a bit weird, because the beginning characters remains the same and only ending changes.
My file is pretty huge and it would be the best for me to stay by my current sorted file. How can I apply the same string comparison in Python?
If it is impossible to implement this comparison quickly, or there might hapen some other issue, how can I sort my file using sort
again but this time with Pythonly correct comparison algorithm?
UPDATE
Example of Python output below (inconsistent with output of Unix sort
tool):
>>> '250,1' > '25,8'
True
>>> '250,' > '25,'
True
Contrary to Unix sort
tool, in Python both comparisons give the same result.