简体   繁体   English

Ruby文件解析每条记录x行

[英]Ruby file parsing x rows per record

I have a text file to parse. 我有一个文本文件要解析。 In this file, each record has content spread over a variable number of lines. 在此文件中,每个记录的内容分布在可变数量的行上。 The number of rows per record is not a fixed number. 每条记录的行数不是固定的。 The content of the file looks like this: 该文件的内容如下所示:

ID\tcontent\tcontent
\tcontent\tcontent
\tcontent\tcontent
\tcontent\tcontent
ID\tcontent\tcontent
\tcontent\tcontent
\tcontent\tcontent
\tcontent\tcontent
\tcontent\tcontent
\tcontent\tcontent
\tcontent\tcontent
\tcontent\tcontent
ID\tcontent\tcontent
\tcontent\tcontent

I want to slice it where there is a record in the first tab column (the ID column is empty in the following lines, so this way to determine a new record should work). 我想将其切片在第一个选项卡列中有记录的位置(在以下各行中ID列为空,因此这种确定新记录的方法应该可以使用)。

My current code for splitting it into chunks of five lines and then merging it: 我当前的代码将其分成五行,然后合并:

f = File.read(file).each_line
f.each_slice(5) do | slice_to_handle |
  merged_row = slice_to_handle.delete("\n").split("\t").collect(&:strip)
  # Dealing with the data here..
end

I need to modify this to slice it as soon as there is an ID set in the first column. 我需要对其进行修改以在第一列中设置ID后立即对其进行切片。

File.read(file)
.split(/^(?!\t)/)
.map{|record| record.split("\t").map(&:strip)}

Result 结果

[
  [
    "ID",
    "content",
    "content",
    "content",
    "content",
    "content",
    "content",
    "content",
    "content"
  ],
  [
    "ID",
    "content",
    "content",
    "content",
    "content",
    "content",
    "content",
    "content",
    "content",
    "content",
    "content",
    "content",
    "content",
    "content",
    "content",
    "content",
    "content"
  ],
  [
    "ID",
    "content",
    "content",
    "content",
    "content"
  ]
]

Ruby's Array inherits from Enumerable, which has slice_before , which is your friend: Ruby的Array继承自Enumerable,它具有slice_before ,是您的朋友:

text_file = "ID\tcontent\tcontent
\tcontent\tcontent
\tcontent\tcontent
\tcontent\tcontent
ID\tcontent\tcontent
\tcontent\tcontent
\tcontent\tcontent
\tcontent\tcontent
\tcontent\tcontent
\tcontent\tcontent
\tcontent\tcontent
\tcontent\tcontent
ID\tcontent\tcontent
\tcontent\tcontent".split("\n")

text_file.slice_before(/^ID/).map(&:join) 

Which looks like: 看起来像:

[
  "ID\tcontent\tcontent\tcontent\tcontent\tcontent\tcontent\tcontent\tcontent",
  "ID\tcontent\tcontent\tcontent\tcontent\tcontent\tcontent\tcontent\tcontent\tcontent\tcontent\tcontent\tcontent\tcontent\tcontent\tcontent\tcontent",
  "ID\tcontent\tcontent\tcontent\tcontent"
]

text_file is an array of lines, similar to what you'd get if you slurped a file using readlines . text_file是一个由行组成的数组,类似于使用readlines抓取文件所得到的内容。

slice_before iterates over the array looking for matches to the /^ID/ pattern, and creates a new sub-array each time it's found. slice_before遍历数组以查找与/^ID/模式的匹配项,并在每次找到时创建一个新的子数组。

map(&:join) walks over the sub-arrays and joins their contents into a single string. map(&:join)遍历子数组,并将其内容连接到单个字符串中。

This is not very scalable though. 不过,这不是很可扩展。 Using it, you'd be relying on being able to slurp in the entire file into memory, which can stop a machine in its tracks. 使用它,您将依赖于将整个文件插入到内存中,这可以使机器停止运行。 Instead, it's better to read the content line-by-line and break the blocks and process them as soon as possible. 取而代之的是,最好逐行读取内容并打破障碍并尽快对其进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM