【问题标题】:Python substring findPython子字符串查找
【发布时间】:2020-10-14 13:12:46
【问题描述】:

我有一个小要求,我需要有关此代码的帮助:

def grepi(dico, fichier):
    line_number = 0
    nameFile = os.path.basename(fichier)
    # Chargement dico
    with open(dico, encoding="utf-8") as dic:
        dicolist = dic.read().splitlines()


    # Recherche dans fichier
    with open(fichier, encoding="utf-8") as fic:
        ficlist = fic.read().splitlines()

    for line in ficlist:
        line_number += 1
        for patt in dicolist:
            line = line.lower()
            if re.search(r' + line + r'\b', patt):
                print(line.rstrip() + ', ' + patt + ', ' + nameFile + ', '
                      + str(line_number))

我在这里遇到了麻烦:if re.search(r' + line + r'\b', patt):

dico 是名字的字典,例如:

benoît
Nicolas
Stéphane
Sébastien
Alexandre

fichier 是一个包含大量信息的文件,例如:

Is the first name of Nicolas
Is Benoît is here
Hey 1234Alexandre1234
   Stéphane found something
dfqklnflSébastiendsqjfldsjfldksj

等等。

在文件中,我想返回所有确切的字符串(即名字)。但是有些名字的格式是这样的:1234Alexandre5678,我找不到只返回Alexandre的方法,对于我想返回Sébastien的dfqklnflSébastiendsqjfldsjfldksj也是一样...

有人可以帮助我吗? 谢谢!

我如何用答案更正我的代码:

#!/usr/bin/env python3
import os
import re


def grepi(dico, fichier):
    line_number = 0
    nameFile = os.path.basename(fichier)
    result_final = []

    dicolist = open(dico, encoding="utf-8").read().splitlines()
    print(dicolist)

    with open(fichier, encoding="utf-8") as ficlist:
        ficstring = ficlist.read().splitlines()
        for line in ficstring:
            ptrn = re.compile(r"\w*(" + "|".join(dicolist) + r")\w*",
                              flags=re.I)
            ptrn_result = ptrn.findall(line)
            if ptrn_result:
                result_final = (nameFile, line_number, str(ptrn.findall(line)))
                print(result_final)
            line_number += 1

这里是输出:

('prénom.xml', 4, "['Benoit']")
('prénom.xml', 6, "['Stéphane']")
('prénom.xml', 9, "['Alexandre']")
('prénom.xml', 10, "['Nicolas']")
('prénom.xml', 14, "['Sébastien']")

【问题讨论】:

    标签: python-3.x search find python-re


    【解决方案1】:

    尝试使用模式'\w*(benoît|Nicolas|Stéphane|Sébastien|Alexandre)\w*'

    例如:

    import re
    
    dicolist = ['benoît', 'Nicolas', 'Stéphane', 'Sébastien', 'Alexandre']
    s = """Is the first name of Nicolas
    Is Benoît is here
    Hey 1234Alexandre1234
       Stéphane found something
    dfqklnflSébastiendsqjfldsjfldksj"""
    
    ptrn = re.compile(r"\w*(" + "|".join(dicolist) + r")\w*", flags=re.I)
    print(ptrn.findall(s))
    

    输出:

    ['Nicolas', 'Benoît', 'Alexandre', 'Stéphane', 'Sébastien']
    

    【讨论】:

    • 我喜欢这段代码!所以我相应地修改了脚本以适应我对您的代码的需求。但它有效!非常感谢
    【解决方案2】:

    哦!哥们,你的第一个函数 grepi() 需要一些缩进。其余的问题对我自己来说也很复杂。

    【讨论】:

    • 对不起!我粘贴代码太快忘记缩进了...
    猜你喜欢
    • 2021-08-13
    • 1970-01-01
    • 2010-10-21
    • 2023-04-03
    • 2012-09-07
    • 2013-10-22
    • 1970-01-01
    • 2014-02-27
    • 2014-03-17
    相关资源
    最近更新 更多