我的机器人从 document.txt 文件中一一读取电子邮件,在使用此电子邮件登录后,机器人会输出我在另一个文件中的评论。
我已经达到机器人读取电子邮件的程度,但我希望特定帐户做出特定而不是重复的评论。
所以我想到了从注释文件中读取特定行的解决方案。
例如,帐户 1 读取并放置评论文件的第 1 行。我想知道如何从注释文件中读取第二行。
这是我一一阅读评论时的代码部分,但我想阅读例如第二行或第三行!
file = 'comments.txt'
File.readlines(file).each do |line|
comment = ["#{line}"]
comment.each { |val|
comment = ["#{val}"]
}
end
Run Code Online (Sandbox Code Playgroud)
尝试下面的方法。
# set the line number to read
line_number = 2 # <== Reading 2nd line
comment = IO.readlines('comments.txt')[line_number-1]
Run Code Online (Sandbox Code Playgroud)