【问题标题】:Unable to sort data from txt flie using sort command in terminal无法使用终端中的排序命令对 txt 文件中的数据进行排序
【发布时间】:2020-04-04 09:24:02
【问题描述】:

使用python代码提取数据,然后在Ubuntu终端打印,然后在Ubuntu终端使用sort命令排序后,99等后面的数据不能正确排序,如9(见下例)。

我在 ubuntu 终端中使用以下命令-->

python3 pythonfile.py file1.txt | sort

如果在ubuntu终端中使用-g参数就可以了-->

python3 pythonfile.py file1.txt | sort -g

但我只想使用 sort 而不是任何其他参数,例如-g(因为我们不允许使用它)。

我只想在 Ubuntu 终端打印数据,然后使用python3 pythonfile.py file1.txt | sort对其进行排序

我不想将数据保存在“list”之类的python代码中,然后对其进行排序,然后在终端中打印。

请提供任何建议如何在 Ubuntu 终端打印后对数据进行排序。

下面是使用python3 pythonfile.py file1.txt | sort后的例子 查看最后一行也没有正确排序

 1. 980 E_4.802337379703114 981 C_-0.5174946578900912
    982 E_-0.04914191239380816 983  D_-0.681508079883169
    983 E_1.847345390268389 986 B_-0.36321505253912734
    986 D_0.8824340792739893 987    A_3.841576990884448
    987 D_2.8497812351372 988   B_4.548331946363086
    988 C_2.3889731803370955 989    B_2.118431924821179
    989 C_-0.07182437212394133 98   C_0.5129253356889238
    98  D_2.4529275958484718 990    B_3.6398151615539627
    990 C_-1.4798828718232553 990   D_3.249621796699633
    991 A_1.9238692496051026 991    D_4.974963953240026
    992 C_1.466211779881152 992 D_0.7144100838397083
    993 B_1.6590910160220598 994    B_3.840419732762326
    996 B_-0.2755606867866667 996   E_1.4732124728343967
    997 B_-0.4377938038287166 997   D_0.595186949529743
    997 E_-1.7359724002373582 998   D_3.502055570086875
    999 A_3.6392659412484214 999    C_-0.35184258330491924
    99  C_2.8983151961646527 99 E_-2.6201821295906322
    9   C_-0.1336234683083135 9 D_2.8068429946499425
    9   E_3.4384330848808187

【问题讨论】:

  • 为什么不对pythonfile.py 中的数据进行排序?它们是如何存储的?请考虑提供file1.txt 的示例,否则我们都很难猜出您的代码和数据有什么问题。
  • 你应该通过在控制台中输入来阅读你的命令文档; man sort.

标签: python sorting ubuntu terminal


【解决方案1】:

您应该使用数字排序:sort -n

python3 pythonfile.py file1.txt | sort -n

【讨论】:

    猜你喜欢
    • 2019-11-19
    • 2020-08-04
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多