【问题标题】:Ignore Lorem Ipsum text in a file Ruby忽略文件 Ruby 中的 Lorem Ipsum 文本
【发布时间】:2020-02-13 11:20:32
【问题描述】:

我有一个 .txt 文件,该文件在一行上有姓氏、名字,在每一行上都有 Lorem Ipsum 文本。我需要在每一行中检测 Lorem Ipsum 并跳过它。

示例 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

【问题讨论】:

  • 您是想跳过每隔一行还是实际检测到标题所暗示的“lorem ipsum”?
  • 每当你给出一个例子时总是显示想要的结果(即返回值)。请编辑。
  • 好的,我稍微编辑了这个问题。让我知道现在是否有意义
  • 当你说 “我需要在每一行中检测 Lorem Ipsum 并跳过它” - “检测 Lorem Ipsum”是什么意思意思是?为什么不跳过每一行?

标签: ruby include


【解决方案1】:

读取 2 行并忽略第二行:

File.open("test.txt", "r") do |f|
  f.each_slice(2) do |odd, _even|
    puts odd
  end
end

【讨论】:

    【解决方案2】:

    如果你只想跳过每一行,你可以这样做:

    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
    

    现在我不太擅长正则表达式,但你也可以这样做来只处理两个单词的行,两个单词都以大写字母开头,用逗号和空格分隔(基本上是名字和姓氏):

    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 文本:

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

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

    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 line 行替换为其他内容。

    【讨论】:

    • i % 2 == 0也可以写成i.even?
    • @3limin4t0r 感谢您的建议
    • 我试图制作一个版本来检查 OP 提供的文件中每一行的完整 Lorem Ipsum 文本,如果它包含在 Lorem 文本中,则跳过该行,但我找不到完整的文本。我发现的每个版本都不包含 OP 问题的第二句话。
    • 我更改了 Lorem Ipsum 文本
    • @downwiththe.click 这些解决方案仍然产生预期的输出:)
    【解决方案3】:

    您的描述不清楚。如果你只是想跳过每一行,你可以这样做:

    File.foreach("test.txt").with_index(1) do |l, i|
      next if i.even?
      puts l
    end
    

    【讨论】:

      【解决方案4】:

      让我们先创建一个文件。

      FName = 'temp.txt'
      

      IO.write(FName,
      <<~END
      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.
      END
      )
        #=> 211 
      

      这是一种每隔一行返回的方法。

      IO.foreach(FName).each_slice(2).map(&:first)
        #=> ["Spade, Kate\n", "Vuitton, Louis\n", "Bucks, Star\n",
        #    "Cage, Nicholas\n", "Brown, James\n"]
      

      请参阅IO::writeIO::foreachEnumerable#each_sliceArray#map

      请注意,foreacheach_slicemap 在没有给定块时都会返回枚举数。因此,我们得到以下结果:

      enum0 = IO.foreach(FName)
        #=> #<Enumerator: IO:foreach("temp.txt")> 
      enum1 = enum0.each_slice(2)
        #=> #<Enumerator: #<Enumerator: IO:foreach("temp.txt")>:each_slice(2)> 
      enum2 = enum1.map
        #=> #<Enumerator: #<Enumerator: #<Enumerator: IO:foreach("temp.txt")>
        #     :each_slice(2)>:map> 
      enum2.each(&:first)
        #=> ["Spade, Kate\n", "Vuitton, Louis\n", "Bucks, Star\n",
        #    "Cage, Nicholas\n", "Brown, James\n"] 
      

      检查enum1enum2 的计算返回值。将这些视为可能会有所帮助,因为这些可以被视为 compound 枚举数。

      另外两种方式:

      enum = [true, false].cycle
        #=> #<Enumerator: [true, false]:cycle> 
      IO.foreach(FName).select { enum.next }
        #=> <as above>
      
      keep = false
      IO.foreach(FName).select { keep = !keep }
        #=> <as above>
      

      【讨论】:

        猜你喜欢
        • 2022-11-27
        • 2013-04-11
        • 2021-12-17
        • 1970-01-01
        • 2013-07-04
        • 2013-07-30
        • 2010-09-10
        • 2021-11-28
        • 2011-04-09
        相关资源
        最近更新 更多