ruby - 解析具有不同分隔符的文本 - 带分组

标签 ruby parsing text

我正在编辑我之前的帖子,因为我取得了一些进展,但现在有点卡住了:

文本文件示例如下。我现在可以读取文件并进行一些解析以获取我需要的数据并输出文件。但是,输出将数据放在不同的行上,我需要将输出文件(名称、到期日期、last_used、address1、address2、city、state、zip)放在以逗号分隔的一行上。

到目前为止的代码如下:

def is_numeric?(object)
true if Float(object) rescue false
end



def load_file
raw_records = []
infile = File.open("testfile.txt", "r") 
#counter =1 

while line = infile.gets
possible_account_number = line[0,16]
if is_numeric?(possible_account_number)
  account_number= possible_account_number[5,11]
  name = line[21,27].strip.gsub(/\,/,"")
  expire_date = line[108,8].strip
  last_used = line[117,8].strip    
  line = infile.gets
  line = infile.gets
  address1 = line.strip.gsub(/\,/,"") #needed for some random commas
  line = infile.gets
  address2 = line.strip.gsub(/\,/,"")
  line = infile.gets
  city = line[21, 20].strip.gsub(/\,/,"")
  state = line[42, 2]
  zip = line[45, 5]
  record = [name, expire_date,last_used, address1, address2, city, state, zip]
  raw_records << record
  #counter = counter + 1
 end

end
infile.close
puts raw_records.map {|record| record*','}

File.open('test_w.txt', 'w') do |f2|   
f2.puts raw_records.map {|record| record*','} 
end


end

#the_string.gsub(/\,/,"")


load_file

这是原始数据:

11111 ABC MOVINGABC, INC                   1234567891 LISTINGS                 02-06-12  MONDAY             2112-001-001  PAGE     1
      1234 CUSTOMIA ROAD  SUITE 12345      LIST MANAGEMENT
      NOSAOLOS        NV 12345


STATEMENTS TISSUE    STATEMENTS NAME 1                 ABC        TISSUES       TISSUE ROAD        LOC      TISSUES  PAGE ABC TISSUE
                     STATEMENTS NAME 2
                     ADDRESS LINE 1
                     ADDRESS LINE 2
                     CITY                 ST ZIP

TITLE   TISSUE NUMBER: 123456789

1234567890000030     MARILYN SMITH                  12345678911                                             05-30-12 01-28-12

                     1234 ST MARYS BLVD.
                     SUITE B
                     NOSAOLOS             MI 12345

1234567890000048     MARILYN ACTIVITA               12345678911                                             05-30-12 09-04-11

                     1234 ST MARYS BOULEVARD
                     STE. B
                     NOSAOLOS             OH 12345

1234567890000055     ANDREW WAYMENT                 12345678911                                             05-30-12 01-12-12

                     123 S. DESCRIBE ST.

                     NOSAOLOS             OH 12345

Here is the finished text- with help from Jason (thanks):

MARILYN SMITH,5-30-12 ,1-28-12,1234 ST MARYS BLVD.,SUITE B,NOSAOLOS,MI,12345
MARILYN ACTIVITA,5-30-12 ,9-04-11,1234 ST MARYS BOULEVARD,STE. B,NOSAOLOS,OH,12345
ANDREW WAYMENT,5-30-12 ,1-12-12,123 S. DESCRIBE ST.,,NOSAOLOS,OH,12345

I also wanted it save to a file and I used this:

File.open('test_w.txt', 'w') do |f2|   
f2.puts raw_records.map {|record| record*','} 
end

安德鲁

最佳答案

如果没有输入文件,很难给您任何代码作为示例,但文件的图像看起来相当可预测,因此具有一些 RegExp 魔法的状态跟踪器应该可以解决问题。

该文件看起来是制表符分隔的,因此您可以按制表符拆分行:

File.open('filename', 'r') do |file|
  lines = file.inject([]){|memo, line| memo.push line.split(/\t/)}
  # Now you have an array of arrays that you can parse with a state tracker
end

您的状态跟踪器将简单地跟踪您最后输入的内容,例如号码、姓名或发布日期,然后填充正确的值。

关于ruby - 解析具有不同分隔符的文本 - 带分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9196199/

相关文章:

ruby - Gem 不加载 config.ru

php - 如何用PHP解析CSV文件并插入到MySQL数据库中?

具有自定义字体的 iOS navigationItem 标题失去垂直居中

ios - 如何使用 Core Graphics 右对齐文本

javascript - 在 JavaScript Canvas 上创建文本轮廓

ruby:迭代期间无法将新 key 添加到哈希中

ruby-on-rails - 安装 RVM - 无输出,失败

ruby - 我正在学习 Ruby,我不清楚 :* means in inject(:*) 是什么

windows - 从delphi中的文本段落中提取第一行

sql-server - 使用 TSQL 解析 XML - 带有嵌套节点问题的 value()