【问题标题】:Automatically remove numbers and alphanumeric words except for those containing "ème"自动删除除包含“ème”的数字和字母数字词
【发布时间】:2017-01-22 04:32:42
【问题描述】:

使用 sed 或我可以在 txt 文件目录上运行的其他程序,我如何从文件中删除纯数字以及数字和字母的组合,除了那些是法语基数(例如 2ème)。

例如,如果一个文本文件包含

12h03 11:00 27.8.16 23 3ème bonjour

那我只想保留

3ème bonjour

编辑: bonjour 被保留,因为其中没有出现数字。 3ème 被保留,因为它以 ème(基数)结尾。其他标记被删除,因为它们包含数字但不是基数。

【问题讨论】:

  • 实际规则是什么?您说您只想要包含“ème”的单词,然后在示例中包含 bonjour 作为要保留的内容。规则集的哪一部分允许在输出中包含“bonjour”?
  • 抱歉不清楚:如果单词中有数字,则应删除,除非它以'ème'结尾。
  • 所以这行“那我只想保留'3ème bonjour'。”应改为“那我只想保留‘3èmer’。”对吗?
  • 不,'bonjour' 不包含任何数字,因此不应删除。
  • 你对sed有多关心?考虑到“或我可以在 txt 文件目录上运行的其他程序”,Python 解决方案是否可以接受?

标签: python-3.x sed


【解决方案1】:

jwpfox:我以前从未使用过 Python,但我愿意使用它。

与此同时,我编写了一个丑陋的猪 R 脚本,它似乎可以很好地满足我的目的。我会在这里分享。

# Sample text
text <- "Le 8 septembre à 11h30, Jean voyait les 2 filles pour la 3ème fois."

# Split up by space
splittext <- unlist(strsplit(text, 
                             split = " "))

# Retain words containing no numbers, or that contain 'ème' or punctuation.
selecttext <- splittext[!(grepl("\\d", splittext)) | 
                        grepl("ème", splittext) |
                        grepl("[[:punct:]]", splittext)]

# If a word contains both numbers and punctuation, retain only the punctuation
selecttext[grepl("\\d", selecttext) & grepl("[[:punct:]]", selecttext)] <- stringr::str_sub(selecttext[grepl("\\d", selecttext) & grepl("[[:punct:]]", selecttext)], start=-1, end =-1)

# Recombine
text2 <- paste(selecttext, collapse = " ")


> text2
[1] "Le septembre à , Jean voyait les filles pour la 3ème fois."

然后应该是读入目录中的所有文件,通过上面的行运行它们,然后覆盖源文件。

【讨论】:

    【解决方案2】:

    由于您愿意接受 Python 答案,因此这里是 Python3 中的答案。

    filepath 变量中提供要处理的树的根目录的路径,这将遍历该目录下树中的所有文件并应用您提供的规则。

    请注意,您在R 代码中应用的规则似乎与您在问题中提出的规则不同。

    import os
    import re
    
    filepath = 'testfiles'
    for(path, dirs, files) in os.walk(filepath):
        searchpattern = re.compile('[0-9]')
        for filename in files:
            curfilepath = os.path.join(path, filename)
            with open(curfilepath, mode='r', encoding='utf-8') as infile:
                cleanlines = []
                for line in infile:
                    cleanline = ''
                    words = line.split(' ')
                    for word in words:
                        if 'ème' in word:
                            cleanline += word + ' '
                        if searchpattern.search(word) is None:
                            cleanline += word + ' '
                    cleanline = cleanline.strip()
                    if len(cleanline) > 0:
                        cleanlines.append(cleanline)
    
            with open(curfilepath, mode='w', encoding='utf-8') as outfile:
                for line in cleanlines:
                    outfile.write(line + '\n')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-15
      • 1970-01-01
      • 1970-01-01
      • 2018-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多