Ruby:CSV到XML(版本2)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ruby:CSV到XML(版本2)相关的知识,希望对你有一定的参考价值。
I use this code snippet all the time. It allows me to parse a CSV file and transform it into an XML file.
#!/usr/bin/ruby =begin filename = 'data.csv' file = File.new(filename, 'r') file.each_line(" ") do |row| columns = row.split(",") break if file.lineno > 10 end =end require 'rubygems' #sudo gem update excelsior require 'excelsior' def read_csv( file ) rows = Array.new Excelsior::Reader.rows( File.open( file , 'r') ) do |row| rows << row end return rows end def generate_xml( array ) output = File.new("CSV_to_XML.xml", "w") output.puts '<?xml version="1.0" encoding="UTF-8"?>' output.puts '<records>' array.each do |record| output.puts " " + '<record name="' + record[0].to_s + '" year="' + record[1].to_s + '" section="' + record[2].to_s + '" tile="' + record[3].to_s + '" award="' + record[4].to_s + '" />' end output.puts '</records>' end rows = read_csv( "scholars_list_FINAL.csv" ) generate_xml( rows )
以上是关于Ruby:CSV到XML(版本2)的主要内容,如果未能解决你的问题,请参考以下文章
将片段中的Firebase信息检索到recyclerview中
在Tomcat的安装目录下conf目录下的server.xml文件中增加一个xml代码片段,该代码片段中每个属性的含义与用途