如何逐行读取gzip文件? [英] How do I read a gzip file line by line?

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

问题描述

  infile = open(file.log.gz )
gz = Zlib :: GzipReader.new(infile)
output = gz.read
puts result

我认为这将文件转换为一个字符串,但我想逐行阅读。



我想要要完成的是该文件有一些垃圾的警告消息,我想grep这些警告消息,然后将其写入到另一个文件。但是,一些警告信息是重复的,所以我必须确保我只grep他们一次。因此,逐行阅读可以帮助我。解决方案

你应该能够简单地循环浏览gzip阅读器流(根据文档

  infile = open(file.log.gz)
gz = Zlib :: GzipReader.new (infile)
gz.each_line do | line |
puts line
end


I have a gzip file and currently I read it like this:

infile = open("file.log.gz")
gz = Zlib::GzipReader.new(infile)
output = gz.read
puts result

I think this converts the file to a string, but I would like to read it line by line.

What I want to accomplish is that the file has some warning messages with some garbage, I want to grep those warning messages and then write them to another file. But, some warning messages are repeated so I have to make sure that i only grep them once. Hence line by line reading would help me.

解决方案

You should be able to simply loop over the gzip reader like you do with regular streams (according to the docs)

infile = open("file.log.gz")
gz = Zlib::GzipReader.new(infile)
gz.each_line do |line|
  puts line
end

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

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