Ruby - 解析文本文件

kay*_*y85 14 ruby

我是Ruby的新手,并且一直在尝试一些非常基本的文本解析.我现在正试图解析一个复杂的文件,然后把它推出到一个csv文件(我之前没有做过),而且我很困难.

该文件如下所示,

Title
some text
some different text
Publisher: name
Published Date: date
Number1: number
Number2: number
Number3: number
Category: category
----------------------
Title
some text
some different text
Publisher: name
Published Date: date
Number1: number
Number2: number
Number3: number
Category: category
----------------------
Run Code Online (Sandbox Code Playgroud)

等等

每行代表csv中的一个新"列".

有人可以帮助吗?

非常感谢!

kur*_*umi 24

以下是您的一般想法

File.open( thefile ).each do |line|
    print line without the new line if line does not contain  /--+/
    if line contains /--+/
        print line with a new line
    end
end
Run Code Online (Sandbox Code Playgroud)


Ela*_*lad 5

这是一个完整的解决方案。请注意,它对文件结构非常敏感!

out_file = File.open('your_csv_file.csv', 'w')
out_file.puts "Title,Publisher,Publishedate,Number1,Number2,Number3,Category"
the_line = []
in_title = false
IO.foreach('your_file_name') do |line|
  if line =~ /^-+$/
    out_file.puts the_line.join(',')
    the_line = []
  elsif line =~ /^Title$/
    in_title = true
  elsif line =~ /^(?:Publishe(?:r|d Date)|Number\d|Category):\s+(.*?)$/
    the_line += [$1]
    in_title = false
  elsif in_title
    the_line[0] = (the_line.empty? ?  line.chomp : "\"#{the_line[0]} #{line.chomp}\"")
  else
    puts "Error: don't know what to do with line #{line}"
  end
end
out_file.close
Run Code Online (Sandbox Code Playgroud)