应用转换后修改一系列行
我想编写一个 kiba 转换,允许我为特定行数插入相同的信息。在这个 如果我有一个包含子标题的 xls 文件,这个子标题也包含数据,如下所示:
Client: John Doe, Code: 1234
qty, date, price
1, 12/12/2017, 300.00
6, 12/12/2017, 300.00
total: 2100
Client: Nick Leitgeb, Code: 2345
qty, date, price
1, 12/12/2017, 100.00
2, 12/12/2017, 200.00
2, 12/12/2017, 50.00
total: 600
Client: …..
为了提取相关数据,我使用了下一个转换,它返回与提供的两个正则表达式中至少一个正则表达式匹配的行 (日期或“客户”一词)
transform, SelectByRegexes regex: [/\d+\/\d+\/\d+/, /Client:/], matches: 1
这将给我下一个结果:
Client: John Doe, Code: 1234
1, 12/12/2017, 300.00
6, 12/12/2017, 300.00
Client: Nick Leitgeb, Code: 2345
1, 12/12/2017, 100.00
2, 12/12/2017, 200.00
2, 12/12/2017, 50.00
…..
现在我有了我想要的信息,我需要为每个子行复制客户端和代码,并删除子标题
John Doe, 1234, 1, 12/12/2017, 300.00
John Doe, 1234, 6, 12/12/2017, 300.00
Nick Leitgeb, 2345, 1, 12/12/2017, 100.00
Nick Leitgeb, 2345, 2, 12/12/2017, 200.00
Nick Leitgeb, 2345, 2, 12/12/2017, 50.00
我能想到的唯一方法是直接在 source
或 pre_process
block 中进行,但需要转换
以前为了显示必要的数据而使用过,是否可以在 source/pre_process block 中使用转换类?,
或在转换中操作多行?
最佳答案
Kiba 作者在这里!感谢您使用 Kiba。你是对的,你可以从专门的 source
实现它,但我个人更喜欢使用以下模式:
last_seen_client_row = nil
logger = Logger.new(STDOUT)
transform do |row|
# detect "Client/Code" rows - pseudo code, adjust as needed
if row[0] =~ /\AClient:\z/
# this is a top-level header, memorize it
last_seen_client_row = row
logger.info "Client boundaries detected for client XXX"
next # remove the row from pipeline
else
# assuming you are working with arrays (I usually prefer Hashes though!) ; make sure to dupe the data to avoid
last_seen_client_row.dup + row
end
end
您当然可以将该 block 转换为更可测试的类,我建议您对行检测非常严格,以确保您检测到格式的任何变化并快速失败。
希望这对您有所帮助!
关于ruby - 应用转换后修改一系列行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42497191/