我有一个我正在阅读的文件,其中包含以下信息:
Organization: QC
Company: Luxury Mortgage Corp. (0020)
Folio: 3366326
Doc Code: QCMAIL_STMT
Sequence: 3
Pages: 7
Method: SCAN
User: LAS
Received: 01/20/2016
我正在尝试从文件中提取行,只使用Folio
,Sequence
,Pages
和User
。
然而,当我这样做时,它会显示如下:
Folio:
Sequence:
Page:
User:
Folio:
Sequence:
Page:
User:
Folio:
Sequence:
Page:
User:
Folio:
Sequence:
Page:
User:
Folio: 3366326
Sequence:
Page:
User:
Folio:
Sequence:
Page:
User:
Folio:
Sequence: 3
Page:
User:
Folio:
Sequence:
Page: 7
User:
Folio:
Sequence:
Page:
User:
Folio:
Sequence:
Page:
User: LAS
我需要的是它出现的喜欢这个:
Folio: 3366326
Sequence: 3
Page: 7
User: LAS
来源:
#!/usr/local/bin/ruby
require 'colored'
class UncommittedDocs
#attr_accessor :file
# def initialize(username)
# @file = file
# end
def pull_info
File.open("/home/qc/tep/bin/ruby/uncomitted.txt", "r").each_line do |line|
puts "Folio: #{line.split(" ")[1] if line.include?("Folio")}"
puts "Sequence: #{line.split(" ")[1] if line.include?("Sequence")}"
puts "Page: #{line.split(" ")[1] if line.include?("Pages")}"
puts "User: #{line.split(" ")[1] if line.include?("User")}"
end
end
end
test = UncommittedDocs.new#"/home/qc/tep/bin/ruby/uncommittedddocstest.txt")
test.pull_info
答案 0 :(得分:2)
你的病情是错误的。
使用此:
puts "Folio: #{line.split(" ")[1] if line.include?("Folio")}"
而不是:
puts "Folio: #{line.split(" ")[1]}" if line.include?("Folio")
答案 1 :(得分:2)
当您真正想要打印符合特定条件的行并跳过其余部分时,似乎您正在使用jasmine.getEnv().addReporter({
jasmineDone: function () {
var HTMLReport = require('jasmine-xml2html-converter');
// Call custom report for html output
testConfig = {
reportTitle: 'Test Execution Report',
outputPath: browser.reportPath,
seleniumServer: 'default',
applicationUrl: browser.baseUrl,
testBrowser: browser.browserName + ' v.' + browser.browserVersion
};
new HTMLReport().from(browser.reportPath + 'junitresults.xml', testConfig);
console.log("... aaaannnnd... done.");
}
});
等进行大量工作:
line.split
如果您要删除前导空格,则以下更改将适应:
class UncommittedDocs
MATCH_LINE_EXPR = /^(Folio|Sequence|Page|User):/
def pull_info
File.open("/home/qc/tep/bin/ruby/uncomitted.txt", "r").each_line do |line|
puts line if line =~ MATCH_LINE_EXPR
end
end
end
test = UncommittedDocs.new
test.pull_info
这只是在正则表达式的开头添加MATCH_LINE_EXPR = /^\s*(Folio|Sequence|Page|User):/
def pull_info
File.open("/home/qc/tep/bin/ruby/uncomitted.txt", "r").each_line do |line|
puts line.lstrip if line =~ MATCH_LINE_EXPR
end
end
(以匹配零个或多个前导空格字符)并将\s*
更改为puts line
以从匹配的行中去除任何前导空格。
要在puts line.lstrip
行后添加空白行,请根据您的评论添加另一个仅在此情况下执行的User:
。由于我们已经在捕获组1中捕获puts
,因此这很简单:
Folio|Sequence|Page|User
答案 2 :(得分:1)
将if
移出字符串插值,以便它适用于puts
次调用:
puts "Folio: #{line.split(" ")[1]}" if line.include?("Folio")
你应该没事。也就是说,直到您遇到包含关键字的数据,例如
User: Mr. Folio Pages
要避免此问题,请使用更严格的条件。您对文件格式的了解越多,就越能定制它。如果您没有任何规范,但只有上述示例文件内容可以使用,那么请查看仍然强大的内容,例如
puts "Folio: #{line.split(" ")[1]}" if line.strip.start_with?("Folio:")
答案 3 :(得分:1)
如果您的输入文件是YAML(看起来可能是这样),请使用Ruby's built-in YAML support:
require 'yaml'
require 'active_support/core_ext/hash/slice' # For comfortably dissecting a Hash
# See https://stackoverflow.com/a/25206082/674064
# ...
def pull_info
data = YAML.load_file('/home/qc/tep/bin/ruby/uncomitted.txt')
puts data.slice('Folio', 'Sequence', 'Pages', 'User').to_yaml
end
# ...
答案 4 :(得分:0)
IO.foreach('/path/to/file.txt') { |l| puts l if l =~ /[Folio|Sequence|Pages|User]: \w/ }