Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为Python排序大型文件_Python_Bash_Sorting_Python 3.7 - Fatal编程技术网

为Python排序大型文件

为Python排序大型文件,python,bash,sorting,python-3.7,Python,Bash,Sorting,Python 3.7,我为Python3实现了一个。问题在于,它需要根据Pythons字符串比较逻辑对文件进行排序。Bash排序不会这样做。例如: aa first aaA second aaa third 此文件根据Python3正确排序,因为“aa”

我为Python3实现了一个。问题在于,它需要根据Pythons字符串比较逻辑对文件进行排序。Bash排序不会这样做。例如:

aa      first
aaA     second
aaa     third
此文件根据Python3正确排序,因为“aa”<“aaA”<“aaA”

如果我使用bash sort-key=1-field separator=$'\t'file.txt对其进行排序,我会得到:

有没有一种有效的方法可以“正确”地为Python排序大型文件


教我的算法bash字符串比较法会更容易吗?

bash顺序取决于您的偏好。您的排序首选项可以在中找到

echo "LC_ALL"
当我使用

export LC_ALL=en_US.UTF-8
你点错菜了。 这可以通过以下方式解决:

export LC_ALL=C

编辑: @mivk回答了一些更正,命令应该是

LC_ALL=C sort --field-separator=$'\t' file.txt
排序使用区域设置的规则进行排序。显然,你需要C型排序

因此,更改您的区域设置,仅针对您的排序命令:

LC_ALL=C sort $your_file
此外,您不需要指定-key=1,因为它是默认值


And-field separator=\t不将其设置为制表符,而是将其设置为普通的t。如果需要指定制表符,可以写-field separator=$'\t.

我不清楚您想要哪种排序方式以及使用哪种工具?python或bash有两种选择,我可以接受:1使用bash进行不同的排序,这样python就没有问题了;2使用bash的算法进行排序,但教授python bash的字符串比较逻辑。太棒了,谢谢你,谢谢你的更正。我将相应地修改我的问题,以避免将来出现混淆。
LC_ALL=C sort --field-separator=$'\t' file.txt
LC_ALL=C sort $your_file