我正在尝试为多个CSV文件创建解析器,最终将以Excel兼容格式输出到另一个CSV文件。 CSV文件由商业工具导出,该工具采用防火墙配置,并向我们报告其发现的任何问题。
到目前为止,我已经想出如何读取文件目录,查找某些值,确定我拥有的设备类型,然后将其吐出到屏幕或CSV,但仅限于每行都有单个单元格条目。如果源IP' cell' (或任何其他)包含多个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文件,但结果是一样的,我认为此代码对于演示此问题更为清晰。
如果有帮助,我可以清理输入文件。
答案 0 :(得分:2)
:
CSV.parse("1,\"2\n\n\",3")
=> [["1", "2\n\n", "3"]]
尝试直接写入documentation中的字符串或文件,以确保引用带换行符的字段:
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"]]