【问题标题】:List of pairs, find those who are not [closed]配对列表,找到那些不是[关闭]
【发布时间】:2016-10-13 19:37:21
【问题描述】:

我读取了文件系统 (Linux) 中的对列表... 唯一文档.xml UniqueDocument.pdf

我需要找到没有xml文件的条目,然后我需要获取它。

一直在尝试使用 os.list 和正则表达式,但在 Ruby 中没有找到合适的解决方案和 Dir()。但我无法走到最后……我的思想阻碍了我。

【问题讨论】:

  • 提供您尝试过的代码以及它是如何失败的。至少提供一些样本输入和预期输出。

标签: python ruby regex


【解决方案1】:

在 Ruby 中,

# Get an array of file names for pdf and xml
pdf=Dir.glob("test/*.pdf").map {|f| File.basename(f, '.pdf')}
xml=Dir.glob("test/*.xml").map {|f| File.basename(f, '.xml')}

# Make the difference between xml and pdf to get file names that have a pdf file but no xml
p pdf - xml

它是如何工作的?

  1. Dir.glob("test/*.pdf")

返回一个数组,其中包含文件夹 test 中所有 pdf 文件的路径。看起来像["test/foo.pdf", ...]

  1. File.basename('test/foo.pdf', '.pdf')

返回不带扩展名的文件名。在这种情况下,将返回'foo'

  1. Dir.glob("test/*.pdf").map {|f| File.basename(f, '.pdf')}

返回一个不带扩展名的文件名数组,只取 pdf 文件。

  1. pdf - xml

返回所有在 pdf 中但不在 xml 中的字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 2013-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多