Manipulating a Variable Length Prefix String

Very new to Python and find tons of great Qs and As on this site. Thank you all. Here is my dilemma:

I have a tree of locations to recursively search and move data of certain types (if they exist), and found many lots for this. My problem is with naming conventions. There is a standard name format in which the prefix is ​​3 or 4 characters (some of them), the name is several, the suffix is ​​four characters. Some names use "_" as a delimiter (PLAN_someFile_to_work_ZZTT or FOR_someOtherFile_XXYY. (PLAN, FOR - prefix .... ZZTT, XXYY - suffix)

Here's the question: how can I split this into three elements purely or β€œPythonicly” into the prefix, fName and Suffix? The length of fName changes and needs a "_", if any. I was able to manipulate and delete the "_", but then it will work with a prefix length of 3 or 4 characters when it comes to getting only fName (with or without "_".) I worked and cannot select the correct number of characters.

Oh, I can extract the penalty prefix and check its existence in a tuple of legal meanings. Is there a way to easily select a Prefix from this Tuple? I could use this instead or try to parse it from a file as an option.

Code is a test code. Print requests are intended to be used in the interests of developers and are not displayed in the final code. Some of the tests are for my understanding of how Py handles this stuff. Thank.

 for dirName, subdirList, fileList in os.walk(rootDir):
    print('Found directory: %s' % dirName)
    if dirName.lower().endswith('.xxx'):
        print('\n %s is a Database. ' % dirName)
        BADrop = ntpath.basename(dirName)
        print ('%s is variable BADrop. ' %BADrop)
        dropName = remove(BADrop, '_')
        print ('%s is variable dropName. ' %dropName)
        NST = BADrop.split('_') [0]
        NSP = BADrop.split('_') [-1]
        NSP = os.path.splitext(NSPrj) [0]
        NSt = os.path.splitext(BADrop) [1]
        abc = dropName[4:-8]
        if NST in pubTheme2: #pubTheme2 is a tuple list of legit values for the Prefix
            print ('%s is variable NST. ' %NST)
            print ('%s is variable NSP. ' %NSP)
            print ('%s is variable NSt. ' %NSt)
            print ('%s is variable abc. ' %abc)
+4
source share
1 answer
x = "PLAN_someFile_to_work_ZZTT"
y = "FOR_someOtherFile_XXYY"

def split(x):
    z = x.split("_")
    z[1] = "_".join(z[1:-1])
    del(z[2:-1])
    return z

print split(x)
print split(y)

Does something like this work for you?

PS, if you do not want the underscores to remain in the "middle" part, just change "_".join(z[1:-1])to "".join(z[1:-1]). Will work anyway.

0
source

All Articles