Html 通过Ruby和CSV打印出多级无序列表

Html 通过Ruby和CSV打印出多级无序列表,html,ruby,csv,Html,Ruby,Csv,这可能是一种倒退的做法。我有一些代码可以读取CSV文件并将结果打印到HTML文件中。如果可能的话,我想把这个文件打印成无序列表 这就是我现在拥有的,它的输出不是我想要的: require 'csv' col_data = [] CSV.foreach("primary_NAICS_code.txt") {|row| col_data << row} begin file = File.open("primary_NAICS_code_html.html", "w")

这可能是一种倒退的做法。我有一些代码可以读取CSV文件并将结果打印到HTML文件中。如果可能的话,我想把这个文件打印成无序列表

这就是我现在拥有的,它的输出不是我想要的:

require 'csv'

 col_data = [] 
 CSV.foreach("primary_NAICS_code.txt") {|row| col_data << row} 

begin
  file = File.open("primary_NAICS_code_html.html", "w")
  col_data.each do |row|
    indentation, (text,*) = row.slice_before(String).to_a
    file.write(indentation.fill("<ul>").join(" ") + "<il>" + text+ "</il></ul?\n")
  end
rescue IOError => e
 puts e
ensure
  file.close unless file == nil
end
需要“csv”
col_data=[]
CSV.foreach(“primary_NAICS_code.txt”){| row | col|u data e
放e
确保
file.close,除非file==nil
结束

  • 无序列表没有被
      包围……输出是什么样子的?您希望它看起来怎么样?一些示例CSV如何?“有关您编写的代码问题的问题必须在问题本身中描述特定问题,并包括重现该问题的有效代码。请参阅以获取指导。”谢谢。这很有帮助。虽然它可以运行几百行代码,但它的统计数据只是为了不断缩进。
      require 'csv'
      
      col_data = [] 
       CSV.foreach("primary_NAICS_code.txt") {|row| col_data << row} 
      
      begin
        file = File.open("primary_NAICS_code_html.html", "w")
        file.write('<ul>')
        depth = 1
        col_data.each do |row|
          indentation, (text,*) = row.slice_before(String).to_a
          if indentation.length > depth
            file.write('<ul>')
          elsif indentation.length < depth
            file.write('</ul>')
          end
          file.write("<li>" + text+ "</li>")
          depth = indentation.length
        end
        file.write('</ul>')
      rescue IOError => e
        puts e
      ensure
        file.close unless file == nil
      end