如何使用Ruby删除文本文件中间的数据行 [英] How do I remove lines of data in the middle of a text file with Ruby

查看:206
本文介绍了如何使用Ruby删除文本文件中间的数据行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何写入文件,并从文件读取,但我不知道如何修改文件,除了将整个文件读入内存,操作它,并重写整个文件。对于大文件,这不是很有效率。

I know how to write to a file, and read from a file, but I don't know how to modify a file besides reading the entire file into memory, manipulating it, and rewriting the entire file. For large files this isn't very productive.

我真的不知道append和write之间的区别。

I don't really know the difference between append and write.

例如

如果我有一个文件包含:

If I have a file containing:

Person1,will,23
Person2,Richard,32
Person3,Mike,44

如何删除包含Person2的行?

How would I be able just to delete line containing Person2?

推荐答案

您可以通过以下几种方式删除一行:

You can delete a line in a several ways:


  • 模拟删除。也就是说,只是用空格覆盖线的内容。稍后,当您阅读和处理文件时,只需忽略此空行。

  • Simulate deletion. That is, just overwrite line's content with spaces. Later, when you read and process the file, just ignore such empty lines.

优点:这很容易,快捷。 缺点:这不是真正的数据删除(文件不缩减),您需要在读取/处理文件时进行更多工作。

Pros: this is easy and fast. Cons: it's not real deletion of data (file doesn't shrink) and you need to do more work when reading/processing the file.

代码:

f = File.new(filename, 'r+')
f.each do |line|
  if should_be_deleted(line)
    # seek back to the beginning of the line.
    f.seek(-line.length, IO::SEEK_CUR)

    # overwrite line with spaces and add a newline char
    f.write(' ' * (line.length - 1))
    f.write("\n")
  end
end
f.close

File.new(filename).each {|line| p line }

# >> "Person1,will,23\n"
# >> "                  \n"
# >> "Person3,Mike,44\n"


  • 这意味着该行将不再存在。所以你必须读取下一行,并覆盖当前行。然后对所有后续行重复此操作,直到到达文件结尾。这似乎是容易出错的任务(不同长度的行等),所以这里是一个无错误的替代方法:打开临时文件,写入行,直到(但不包括)要删除的行,跳过行想要删除,剩下的写入temp文件。删除原始文件并重命名临时文件以使用其名称。完成。

  • Do real deletion. This means that line will no longer exist. So you will have to read next line and overwrite the current line with it. Then repeat this for all following lines until the end of file is reached. This seems to be error prone task (lines of different lengths, etc), so here's an error-free alternative: open temp file, write to it lines up to (but not including) the line you want to delete, skip the line you want to delete, write the rest to the temp file. Delete the original file and rename temporary one to use its name. Done.

    虽然这在技术上是一个完全重写的文件,它不同于你问。该文件不需要完全加载到内存。您一次只需要一行。 Ruby提供了一个方法: IO#each_line

    While this is technically a total rewrite of the file, it does differ from what you asked. The file doesn't need to be loaded fully to memory. You need only one line at a time. Ruby provides a method for this: IO#each_line.

    优点:没有假设。行被删除。读取代码不需要更改。 缺点:删除此行时还需要更多工作(不仅仅是代码,还包括IO / CPU时间)。

    Pros: No assumptions. Lines get deleted. Reading code needs not to be altered. Cons: lots more work when deleting the line (not only the code, but also IO/CPU time).

    在@ azgult的回答中说明了此方法。

    There is a snippet that illustrates this approach in @azgult's answer.

    这篇关于如何使用Ruby删除文本文件中间的数据行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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