本文介绍了忽略文件Ruby中的Lorem Ipsum文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.txt文件,该文件的姓,名在一行中,每隔一行都有Lorem Ipsum文本.我需要每隔一行检测一次Lorem Ipsum,然后跳过它.

I have a .txt file that has last name, first name on one line and on every other line I have Lorem Ipsum text. I need to detect the Lorem Ipsum in every other line and skip it.

示例txt.file

example txt.file

Spade, Kate
Voluptatem ipsam et at.
Vuitton, Louis
Facere et necessitatibus animi.
Bucks, Star
Eveniet temporibus ducimus amet eaque.
Cage, Nicholas
Unde voluptas sit fugit.
Brown, James
Maiores ab officia sed.

预期输出:

#Spade, Kate
#Vuitton, Louis
#Bucks, Star
#Cage, Nicholas
#Brown, James

推荐答案

如果只想跳过第二行,则可以执行以下操作:

If you just want to skip every second line you can do something like this:

File.open("text.txt", "r") do |f|
  f.each_line.with_index do |line, i|
    next unless i.even?
    puts line
  end
end

#Spade, Kate
#Vuitton, Louis
#Bucks, Star
#Cage, Nicholas
#Brown, James

现在我对regexp不太满意,但是您也可以执行以下操作来仅处理两个单词的行,两个行都以大写字母开头,并用逗号和空格分隔(基本上是名字和姓氏)):

Now I'm not really good with regexp, but you could also do something like this to process only the lines that are two words, both starting with a capital letter separated by a comma and space (basically first name and last name):

File.open("text.txt", "r") do |f|
  f.each_line do |line|
    next unless line =~ /[A-Z][a-z]+, [A-Z][a-z]+/
    puts line
  end
end

#Spade, Kate
#Vuitton, Louis
#Bucks, Star
#Cage, Nicholas
#Brown, James

您还可以从这样的文件中加载完整的Lorem Ipsum文本:

You could also load the full Lorem Ipsum text from a file like this:

lorem = File.open("lorem.txt", "r").map(&:chomp).join(" ")

然后检查每一行是否包含在Lorem Ipsum文本中:

And then check each line if it's contained in the Lorem Ipsum text:

File.open("text.txt", "r") do |f|
  f.each_line do |line|
    next if lorem.include?(line[0...-1]) #removing the last character because you seem to have a dot at the end even though in the lorem text there's no dot on these positions.
    puts line
  end
end

#Spade, Kate
#Vuitton, Louis
#Bucks, Star
#Cage, Nicholas
#Brown, James

现在,根据要对数据执行的操作,可以将 puts行行替换为其他内容.

Now depending on what you want to do with the data you can replace the puts line line with something else.

这篇关于忽略文件Ruby中的Lorem Ipsum文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 00:20