【问题标题】:Delete unwanted records from BIG File using Python or Map reduce使用 Python 或 Map reduce 从 BIG 文件中删除不需要的记录
【发布时间】:2018-10-08 20:55:56
【问题描述】:

我在源文件中有 37 M 条记录,但目标只有 3000 条记录,我想删除其他记录,只需要这 3000 条记录。 Python中有什么最快的解决方案?

【问题讨论】:

  • 你必须向我们展示你到目前为止所做的事情。

标签: python file mapreduce compare bigdata


【解决方案1】:

我不知道是删除你不想要的东西更容易还是只保留你想要的东西更容易,因为你没有提供任何示例数据。无论如何,我刚刚测试了下面的脚本,它对我来说很好。

with open("C:\\path_here\\test.txt","r+") as f:
    new_f = f.readlines()
    f.seek(0)
    for line in new_f:
        if "return_3y" in line:
            f.write(line)
    f.truncate()

在一个 5MB 的文件上,只需一秒钟即可完成这项工作。对于您的 37MB 文件,我猜这需要几秒钟才能完成所有操作。还不错……

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-21
    • 2013-12-03
    相关资源
    最近更新 更多