Ruby on rails 如何通过解析文本文件仅显示不匹配的结果?
我有一种方法,可以搜索文本文件中的行,并根据单词列表将它们存储在哈希中 该方法做两件简单的事情: 如果存在匹配项,则使用正则表达式将行存储在“已找到”类别中,否则将结果存储在“未找到”类别中 我的问题涉及“未找到”部分:每一行都将进入未分类状态。我需要的是,未分类事务只能是不在单词列表中的行 这是我的词表:Ruby on rails 如何通过解析文本文件仅显示不匹配的结果?,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有一种方法,可以搜索文本文件中的行,并根据单词列表将它们存储在哈希中 该方法做两件简单的事情: 如果存在匹配项,则使用正则表达式将行存储在“已找到”类别中,否则将结果存储在“未找到”类别中 我的问题涉及“未找到”部分:每一行都将进入未分类状态。我需要的是,未分类事务只能是不在单词列表中的行 这是我的词表: words_to_check = ['BUILDING','LAKE','TREE'] 这是我的文本路径: path_to_file = "/Users/name/Desktop/path
words_to_check = ['BUILDING','LAKE','TREE']
这是我的文本路径:
path_to_file = "/Users/name/Desktop/path_to_file"
文件内容示例:
07/08/2013,"BUILDING",,100.00
07/08/2013,"LAKE",,50.00
07/08/2013,"TREE",,5.50
07/08/2013,"CAT",,10.50
07/08/2013,"DOG",,-19.87
这是构建哈希的方法:
def build_hash(path_to_file, words_to_check)
trans_info = {
:found => {},
:unfound => {}
}
found = trans_info[:found]
unfound = trans_info[:unfound]
words_to_check.each do |word|
found[word] = []
unfound[:unfound] = []
File.foreach(path_to_file) do |line|
if line.include?(word)
date = /(?<Month>\d{1,2})\D(?<Day>\d{2})\D(?<Year>\d{4})/.match(line).to_s
transaction = /(?<transaction>)#{word}/.match(line).to_s
amount =/-+(?<dollars>\d+)\.(?<cents>\d+)/.match(line).to_s.to_f.round(2)
# found word on list now push to array with hash keys
found[word] << {
date: date,
transaction: transaction,
amount: amount
}
else
date = /(?<Month>\d{1,2})\D(?<Day>\d{2})\D(?<Year>\d{4})/.match(line).to_s
transaction = /(?<Middle>)".*"/.match(line).to_s
amount =/-*(?<dollars>\d+)\.(?<cents>\d+)/.match(line).to_s.to_f.round(2)
# push to unfound part of hash
unfound[:unfound] << {
date: date,
transaction: transaction,
amount: amount
}
end
end
end
#found and unfound key/values will be returned
return trans_info
end
def build_hash(路径到文件,单词到检查)
传输信息={
:found=>{},
:unfound=>{}
}
已找到=传输信息[:已找到]
unfound=传输信息[:unfound]
每个单词都要检查|
找到[word]=[]
未找到[:未找到]=[]
foreach(路径到文件)do | line |
如果行。包括?(word)
日期=/(?\d{1,2})\d(?\d{2})\d(?\d{4})/。匹配(行)。到
事务=/(?)35;{word}/.match(line).to_s
金额=/-+(?\d+)\(?\d+)/。匹配(行)。到第二轮(2)
#在列表中找到单词现在使用哈希键推送到数组
找到[word]这与如何设置循环有关。由于要独立检查每个单词,因此本质上要求列表中的所有单词必须排成一行,以避免进入:unfound
类别
作为示例,请查看数据文件的第一行
07/08/2013,"BUILDING",,100.00
在第一次通过words\u to\u检查时。每个循环都会将该行与列表中的第一个单词进行比较,即BUILDING
。这显然是一个匹配项,因此该行被添加到:found
类别中。然而,还有两个词需要比较。在循环的第二次遍历中,您将同一行与单词LAKE
进行比较,因此匹配失败,该行被添加到:unfound
类别中。然后,单词树
也会发生同样的情况。现在程序终于开始检查下一行了
您还必须多次读取该文件,因为文件循环位于单词循环中。由于读取文件的速度非常慢,所以我会颠倒这些循环的顺序。也就是说,我会把loop这个词放在里面
您可能希望更像这样构造您的循环:
File.foreach(path_to_file) do |line|
line_does_match = false # assume that we start without a match
words_to_check.each do |word| # check the current line against all words
if line.include? word
line_does_match = true # record that we have a match
break # stop the words_to_check.each loop
end
end
# Now that we've determined whether the line matches ANY of the
# words in the list we can deal with it accordingly.
if line_does_match
# add it to the :found list
else
# add it to the :unfound list
end
end
谢谢你抽出时间。我认为这会起作用,但它不会将多个键或单词组合在一起。我想我以后可以把清单分类。例如,我最终想把所有“树”结果组合在一起。谢谢你的建议,不过我会尝试一下。你应该能够接受一般的想法并修改它以满足你的需要。需要记住的关键是,由于涉及到两个循环,所以不能将每个比较都视为独立的东西。在外循环的每个过程中发生的每个内循环运行都是相关的。(我希望这有道理……)非常感谢你的耐心。我一直在努力编写这段代码(尽管可能很简单)。我会研究你的建议-再次感谢!