CSVparsing,换行符/换行符问题

我正在尝试为多个CSV文件创build一个parsing器,最终将以Excel兼容的格式输出到另一个CSV文件。 CSV文件通过采用防火墙configuration的商业工具导出,并向我们报告发现的任何问题。

到目前为止,我已经想出了如何读取文件目录,查找特定值,确定设备types,然后将其吐出到屏幕或CSV,但只有每行都有单个单元格条目。 如果源IP“单元”(或任何其他)包含多个由换行符隔开的IP,则输出在该换行符上断开,并将剩余部分推送到下一行。

我到目前为止的代码是:

require 'csv' require 'pp' nipperfiles = Dir.glob(ARGV[0] + '/*.csv') def allcsv(nipperfiles) filearray = [] nipperfiles.each do |csv| filearray << csv end filearray end def devicetype(filelist) filelist.each do |f| CSV.foreach(f, :headers => true, :force_quotes => true) do |row| if row["Table"] =~ /audit device list/ && row["Device"] =~ /Cisco/ return "Cisco" elsif row["Table"] =~ /audit device list/ && row["Device"] =~ /Dell/ return "Sonicwall" elsif row["Table"] =~ /audit device list/ && row["Device"] =~ /Juniper/ return "Juniper" end end end end def adminservices(device, filelist) administrative = [] filelist.each do |f| CSV.foreach(f, :headers => true, :col_sep => ",", :force_quotes => true, :encoding => Encoding::UTF_8) do |row| if row["Table"] =~ /administrative service rule/ if row["Dst Port"] != "Any" and row["Service"] != "[Host] Any" if device == "Cisco" administrative << row["Table"] + ',' + row["Rule"] + ',' + row["Protocol"] + ',' + row["Source"] + ',' + row["Destination"] + ',' + row["Dst Port"] elsif device == "Sonicwall" administrative << row["Table"] + ',' + row["Rule"] + ',' + row["Source"] + ',' + row["Destination"] + ',' + row["Service"] elsif device == "Juniper" administrative << row["Table"] + ',' + row["Rule"] + ',' + row["Source"] + ',' + row["Destination"] + ',' + row["Service"] end end end end end administrative end def writecsv(admin) finalcsv = File.new("randomstorm.csv", "w+") finalcsv.puts("Administrative Services Table:\n", admin, "\r\n") finalcsv.close end filelist = allcsv(nipperfiles) device = devicetype(filelist) adminservices(device, filelist) admin = adminservices(device, filelist) writecsv(admin) 

有没有办法让它忽略单元格内的换行符,还是我的代码完整的球,需要重新启动?

我曾尝试使用CSV库编写一个CSV文件,但结果是一样的,我认为这个代码稍微清晰一点,可以certificate这个问题。

如果有帮助,我可以清理input文件。

只要他们被引用,换行符就可以在字段里面了:

 CSV.parse("1,\"2\n\n\",3") => [["1", "2\n\n", "3"]] 

尝试直接写入string或文件中的文件 ,这将确保你的领域与换行符被引用:

 def writecsv(admin) csv_string = CSV.generate do |csv| admin.each { |row| csv << row } end finalcsv = File.new("randomstorm.csv", "w+") finalcsv.puts("Administrative Services Table:\n", csv_string, "\r\n") finalcsv.close end 

还要确保你正在将你的字段写成adminservices()一个数组:

 administrative << [row["Table"], row["Rule"], row["Protocol"], row["Source"], row["Destination"], row["Dst Port"]]