我有一个CSV,我喜欢在其上保存所有哈希值。我使用nokogiri sax来解析xml文档,然后将其保存为CSV。
sax解析器:
require 'rubygems'
require 'nokogiri'
require 'csv'
class MyDocument < Nokogiri::XML::SAX::Document
HEADERS = [ :titles, :identifier, :typeOfLevel, :typeOfResponsibleBody,
:type, :exact, :degree, :academic, :code, :text ]
def initialize
@infodata = {}
@infodata[:titles] = Array.new([])
end
def start_element(name, attrs)
@attrs = attrs
@content = ''
end
def end_element(name)
if name == 'title'
Hash[@attrs]["xml:lang"]
@infodata[:titles] << @content
@content = nil
end
if name == 'identifier'
@infodata[:identifier] = @content
@content = nil
end
if name == 'typeOfLevel'
@infodata[:typeOfLevel] = @content
@content = nil
end
if name == 'typeOfResponsibleBody'
@infodata[:typeOfResponsibleBody] = @content
@content = nil
end
if name == 'type'
@infodata[:type] = @content
@content = nil
end
if name == 'exact'
@infodata[:exact] = @content
@content = nil
end
if name == 'degree'
@infodata[:degree] = @content
@content = nil
end
if name == 'academic'
@infodata[:academic] = @content
@content = nil
end
if name == 'code'
Hash[@attrs]['source="vhs"']
@infodata[:code] = @content
@content = nil
end
if name == 'ct:text'
@infodata[:beskrivning] = @content
@content = nil
end
end
def characters(string)
@content << string if @content
end
def cdata_block(string)
characters(string)
end
def end_document
File.open("infodata.csv", "ab") do |f|
csv = CSV.generate_line(HEADERS.map {|h| @infodata[h] })
csv << "\n"
f.write(csv)
end
end
end
为存储在文件夹中的每个文件(47.000xml文件)创建新对象:
parser = Nokogiri::XML::SAX::Parser.new(MyDocument.new)
counter = 0
Dir.glob('/Users/macbookpro/Desktop/sax/info_xml/*.xml') do |item|
parser.parse(File.open(item, 'rb'))
counter += 1
puts "Writing file nr: #{counter}"
end
问题: 我没有为每一组新的值获得新的一行。有什么想法吗?
用于尝试代码的3个xml文件: https://gist.github.com/2378898 https://gist.github.com/2378901 https://gist.github.com/2378904
答案 0 :(得分:3)
您需要使用“a”模式打开文件(打开带有“w”的文件会清除以前的任何内容)。
将数组附加到csv对象将自动插入换行符。 Hash#values返回值的数组,但强制下订单会更安全。展平数组可能会导致列错位(例如[[:title1,:title2],'other-value']将导致[:title1,:title2,'other-value'])。尝试这样的事情:
HEADERS = [:titles, :identifier, ...]
def end_document
# with ruby 1.8.7
File.open("infodata.csv", "ab") do |f|
csv = CSV.generate_line(HEADERS.map { |h| @infodata[h] })
csv << "\n"
f.write(csv)
end
# with ruby 1.9.x
CSV.open("infodata.csv", "ab") do |csv|
csv << HEADERS.map { |h| @infodata[h] }
end
end
执行以下操作可以验证上述更改:
require "csv"
class CsvAppender
HEADERS = [ :titles, :identifier, :typeOfLevel, :typeOfResponsibleBody, :type,
:exact, :degree, :academic, :code, :text ]
def initialize
@infodata = { :titles => ["t1", "t2"], :identifier => 0 }
end
def end_document
@infodata[:identifier] += 1
# with ruby 1.8.7
File.open("infodata.csv", "ab") do |f|
csv = CSV.generate_line(HEADERS.map { |h| @infodata[h] })
csv << "\n"
f.write(csv)
end
# with ruby 1.9.x
#CSV.open("infodata.csv", "ab") do |csv|
# csv << HEADERS.map { |h| @infodata[h] }
#end
end
end
appender = CsvAppender.new
3.times do
appender.end_document
end
File.read("infodata.csv").split("\n").each do |line|
puts line
end
运行上述后,infodata.csv文件将包含:
"[""t1"", ""t2""]",1,,,,,,,,
"[""t1"", ""t2""]",2,,,,,,,,
"[""t1"", ""t2""]",3,,,,,,,,
答案 1 :(得分:1)
我想你需要一个额外的循环。类似于
的东西CSV.open("infodata.csv", "wb") do |csv|
csv << @infodata.keys
@infodata.each do |key, value|
csv << value
end
end