如何对文本文件逐行排序 [英] How to sort a text file line-by-line

查看:67
本文介绍了如何对文本文件逐行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要按升序对文本文件进行排序.文本文件的每一行都以一个索引开头,如下所示:

I need to sort a text file in ascending order. Each line of the text file starts with an index, as seen below:

2       0       4         0d 07:00:38.0400009155273
3       0       4         0d 07:00:38.0400009155273
1       0       4         0d 07:00:38.0400009155273   

想法结果如下:

1       0       4         0d 07:00:38.0400009155273
2       0       4         0d 07:00:38.0400009155273
3       0       4         0d 07:00:38.0400009155273 

请注意,这个文本文件有 +300 万行,每个元素自然被认为是一个字符串.

Please note, this text file has +3 million rows and each element is naturally considered a string.

我一直在纠结这个问题,但没有任何运气,所以我认为是时候咨询专家了.谢谢你的时间!

I've been messing around with this for sometime now without any luck so I figured it was time to consult with the experts. Thank you for you time!

我在 Spyder IDE 中使用带有 Python 3.7 的 Windows 操作系统.该文件不是 CSV,而是由制表符分隔的文本文件.有可能并非所有索引都存在.原谅菜鸟,我没有很多编码经验.

I'm using windows OS with Python 3.7 in Spyder IDE. The file is not a CSV its a text file that is tab delimited. There is the possibility that not all indices are present. Forgive the noob-ness, I haven't got a lot of experience coding.

推荐答案

fn = 'filename.txt'
sorted_fn = 'sorted_filename.txt'

with open(fn,'r') as first_file:
    rows = first_file.readlines()
    sorted_rows = sorted(rows, key=lambda x: int(x.split()[0]), reverse=False)
    with open(sorted_fn,'w') as second_file:
        for row in sorted_rows:
            second_file.write(row)

这应该适用于 3+ 百万行的文本文件.使用 int(x.split()[0]) 将每行中的第一项作为整数排序

This should work for a text file of 3+ million rows. Using int(x.split()[0]) will sort the first item in each row as an integer

编辑以删除 close() 语句

Edited to remove close() statements

这篇关于如何对文本文件逐行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆