将标题字符串与文件名匹配

时间:2018-09-18 15:44:21

标签: python-3.x

我正在尝试遍历图像文件的目录,并将文件名与相应的标题字符串匹配,以便可以将路径插入数据库。我下面的内容适用于大约90%的文件,但是我的字符串清理方法不完善。我正在寻找一种更智能的方式来做到这一点。感谢您的宝贵时间。

文件名示例:"2020_Super_Baseball_usa.jpg" 标题字符串示例:"2020 Super Baseball"

def string_cleaner(string_one):
'''filter non alpha chars from filename'''

    filter_by= ":*/?-_.,'!'"
    new_string= ""

    for char in filter_by:
        for letter in string_one:
            if letter == char:
                if new_string == "":
                    new_string = string_one.replace(char, "")    
                else:
                    new_string = new_string.replace(char, "")

    return new_string

def matcher(string):
'''matches title string to filename and returns full path'''

    temp_path = ""
    tempfilename = ""
    path = "C:\\Users\\USER\\Desktop\\pyscripts\\web- scraping\\SNES_DB_Scrape\\snes\\"
    for file in os.listdir(path):
        tempfilename = string_cleaner(file[:-7]).lower().lstrip().rstrip()
        if string.lower().lstrip() == tempfilename :
            temp_path = os.path.join(path, file)

    return temp_path 

for name in nameList:
    path = matcher(name)
    print("Name is: " + name + '\n' + "path is : " + path)  

1 个答案:

答案 0 :(得分:0)

考虑您的where (p.FirstName = sub_p.FirstName and p.LastName = sub_p.LastName and p.Dob = sub_p.Dob and p.GtId <> sub_p.GtId) or (a.MailingAddress = sub_a.MailingAddress and a.GtId <> sub_a.GtId) filename示例,您可以尝试以下代码:

title