解析一个Zip文件并从文本文件中提取记录 [英] Parsing a Zip file and extracting records from text files

查看:49
本文介绍了解析一个Zip文件并从文本文件中提取记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的是Ruby新手,可以在程序上使用一些帮助.我需要打开一个包含多个文本文件的zip文件,该文本文件具有很多数据行(例如)

I am really new to Ruby and could use some help with a program. I need to open a zip file that contains multiple text files that has many rows of data (eg.)

CDI|3|3|20100515000000|20100515153000|2008|XXXXX4791|0.00|0.00
CDI|3|3|20100515000000|20100515153000|2008|XXXXX5648|0.00|0.00
CHO|3|3|20100515000000|20100515153000|2114|XXXXX3276|0.00|0.00
CHO|3|3|20100515000000|20100515153000|2114|XXXXX4342|0.00|0.00
MITR|3|3|20100515000000|20100515153000|0000|XXXXX7832|0.00|0.00
HR|3|3|20100515000000|20100515153000|1114|XXXXX0238|0.00|0.00

我首先需要提取zip文件,读取zip文件中的文本文件,然后仅将以( CDI CHO )开头的完整行写入两个输出文件,一个输出文件以 CDI 开头的数据行,另一个输出文件以 CHO 开头的数据行(基本上是分析文件).我必须使用Ruby进行操作,并可能尝试将程序设置为自动功能,以使相同身材的连续zip文件到达.我完全感谢任何人通过任何示例提供的任何建议,指导或帮助.

I first need to extract the zip file, read the text files located in the zip file and write only the complete rows that start with (CDI and CHO) to two output files, one for the rows of data starting with CDI and one for the rows of data starting with CHO (basically parsing the file). I have to do it with Ruby and possibly try to set the program to an auto function for arrival of continuous zip files of the same stature. I completely appreciate any advice, direction or help via some sample anyone can give.

推荐答案

一种方法是使用 ZipFile 库.

require 'zip/zip'

# To open the zip file and pass each entry to a block
Zip::ZipFile.foreach(path_to_zip) do |text_file|
   # Read from entry, turn String into Array, and pass to block
   text_file.read.split("\n").each do |line|
      if line.start_with?("CDI") || line.start_with?("CHO")
         # Do something
      end
   end
end

这篇关于解析一个Zip文件并从文本文件中提取记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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