我有这段代码从csv文件中提取数据然后重新格式化,以便可以与其他数据集进行比较:
def dataExtract
dates = File.open(@filename_data).read.scan /\d{2}\/\d{2}\/\d{2}/
data_extracted = []
index = 0
dates.each do |date|
inbound_row = @data[4+(11*index)]
outbound_row = @data[6+(11*index)]
data_extracted.push [date, '4001', (inbound_row[1].gsub(/\,/,"").to_i + inbound_row[2].gsub(/\,/,"").to_i).to_s, 'AI', 'INBOUND']
data_extracted.push [date, '4090', inbound_row[3].gsub(/\,/,""), 'AI', 'INBOUND']
data_extracted.push [date, '1139', inbound_row[4].gsub(/\,/,""), 'RU STANDRD', 'INBOUND']
data_extracted.push [date, '1158', inbound_row[5].gsub(/\,/,""), 'RU STANDRD', 'INBOUND']
data_extracted.push [date, '4055', outbound_row[1].gsub(/\,/,""), 'RU PLUS', 'OUTBOUND']
data_extracted.push [date, '4055', outbound_row[2].gsub(/\,/,""), 'AR', 'OUTBOUND']
data_extracted.push [date, '1139', outbound_row[4].gsub(/\,/,""), 'RU STANDRD', 'OUTBOUND']
data_extracted.push [date, '1158', outbound_row[5].gsub(/\,/,""), 'RU STANDRD', 'OUTBOUND']
data_extracted.push [date, '4091', outbound_row[3].gsub(/\,/,""), 'RU STANDRD', 'OUTBOUND']
index += 1
end
return data_extracted
end
以下是csv数据的示例(这是一天。多天有像这样的块,它们之间有空格):
Date,BLOCK,,Wood,Miscellaneous,,Totals,MO
Monday,4055-RU,4055-AR,4091,1139,1158,,100
11/4/15,C Sort,B,C,iGPS,PECO,,
Starting,714,228,858,82,129,"2,011",
Sorted,"2,738",190,"1,110",144,228,"4,410",
Subtotal 1,"3,452",418,"1,968",226,357,"6,421",
Shipped,"2,700",0,"1,865",0,0,"4,565",
,752,418,103,226,357,"1,856",
Physical,752,418,103,226,357,"1,856",
Variance,0,0,0,0,0,0,
此csv文件中使用的唯一数据(除日期外)是已排序和已装运的行。无论如何,就像我说的,这是有效的,它不是很漂亮。是否有更好的方法来执行dates.each块,因为有重复的信息(日期在每个数组+入站/出站)?
答案 0 :(得分:2)
将File.open(filename).read
替换为File.read(filename)
,并使用each_with_index
而不是index += 1
,并在结尾处删除多余的return
,然后使用{{ 1}}用于方法名称:
snake_case
答案 1 :(得分:2)
也许更简单,也许不是 - 但希望足以让你知道如何继续改进它。
data_extracted = []
inbound_data_set = [['4090',3,'AI'],['1139',4,'RU STANDRD'],['1158',5,'RU STANDRD']]
outbound_data_set = [['4055',1,'RU PLUS'],['4055',2,'AR'],['1139',4,'RU STANDRD'],['1158',5,'RU STANDRD'],['4091',3,'RU STANDRD']]
dates.each_with_index do |date,index|
inbound_row = @data[4+(11*index)]
outbound_row = @data[6+(11*index)]
# this one stays as it's much different
data_extracted << [date, '4001', (inbound_row[1].gsub(/\,/,"").to_i + inbound_row[2].gsub(/\,/,"").to_i).to_s, 'AI', 'INBOUND']
inbound_data_set.each do |num,idx,type|
data_extracted << [date, num, (inbound_row[idx].gsub(/\,/,"")), type, 'INBOUND']
end
outbound_data_set.each do |num,idx,type|
data_extracted << [date, num, (outbound_row[idx].gsub(/\,/,"")), type, 'OUTBOUND']
end
end
编辑:代码缺少')'on(inbound_row [idx] .gsub(/ \,/,“”))和(outbound_row [idx] .gsub(/ \,/,“”))。