vendredi 21 avril 2017

Is there a more elagent wat to seach every char from 1 string for every char in a 2nd string?

I am adding a feature to a program that will let the user add new dictionaries as a file to the file folder to be used as a note taking repository. I just got the code finished for creating the file name from user input however it feels as if there should be a smarter way to perform the task than what I have done. I have looked around and seen some one liners and other option however I can not get the one liners to work for some reason and the other options don't seam to function correctly because they are not comparing the stings properly.

The following is what I have come up with that does work but I don't know if I should be doing this in a shorter method with either a built in function or a smarter line of code.

If you do have a smarter solution could you please explain it. Just posting the solution will not help me understand. This has been the problem when reading other post on the matter.

finalFilename =''
def new_lib_prompt(event=None):
    finalFilename =''
    name_of_new_library = tkinter.simpledialog.askstring('Create New Note Library', 'Alphanumeric lowercase and "_" only', initialvalue = "Name_Here")
    from string import ascii_letters,digits
    validFilenameChars = "-_.() %s%s" %(ascii_letters, digits)
    stringofnewname = str(name_of_new_library)
    stringofvalid = str(validFilenameChars)
    for i in stringofnewname:
        if i in stringofvalid:
            if i == ' ':
                new_i = '_'
                finalFilename += ''.join(new_i)
            else:
                lower_i = i.lower()
                finalFilename += ''.join(lower_i)
        else:
            pass
    print (finalFilename)

Aucun commentaire:

Enregistrer un commentaire