0

I am learning as I go and came across a little snag. I'm trying to search an inputted string in a dictionary.

example: I want to pull up all episodes of a single season of a show with this search "S*1*Show title". I want it to show S.01 E.01 Show Title S.01 E.02 Show Title ...S.01 E.10 Show Title.

I can get it to show all titles by just putting in "Show title" but this also brings up Season 2, Season 3 and extras.

@app.route('/title', methods=['GET', 'POST'])
def showIPTitle():
    savelist = []
    savelen = str(len(savelist))
    savedlist = [ipheader]
    if request.method=='POST':
        catTitle=request.form['title'].lower()
        savedOp=request.form['titlesaveOp']
        saveloc = saveLocat+'title'+catTitle+'.'+timeString+'.csv'

        if len(catTitle) > 3:
            for k, v in Content.items():
                if re.search(catTitle,str(v['assetName'].lower())):
                    asstitle = str(v['assetName'])
                    assstart = (v['LicenseWindowStart'])[8:18]
                    assend = (v['LicenseWindowEnd'])[8:18]
                    providerid = (v['ProviderID'])
                    asssetid = (v['AssetID'].upper())
                    categories = (v['categories'])
                    savelist.append([asssetid,providerid,asstitle,categories,assstart,assend])
                    savedlist.append([asssetid,providerid,asstitle,categories,assstart,assend])
                    savelen = str(len(savelist))
                    if savelen > 1:
                        if savedOp== 'yesSave':
                            savedfile = open(saveloc, 'w')
                            swriter = csv.writer(savedfile, delimiter = '|')
                            swriter.writerows(savedlist)
                            savedfile.close()
                        else:
                            savedOp =="no"
                else:
                    onstorefront = 'No'
                    asstitle = 'NOT FOUND'
                    assstart = 'NOT FOUND'
                    assend = 'NOT FOUND'
                    providerid = 'NOT FOUND'
                    categories = 'NOT FOUND'
            return render_template('Title.html',selected='search',
            savedloc=saveloc,
            saveOp=savedOp,
            savelist=savelist,
            savelen=savelen,
            enteredID=catTitle,
            header=header,
            asstitle=asstitle,
            assstart=assstart,
            assend=assend,
            providerid=providerid,
            categories=categories,
            method=request.method)
        else:
            return render_template('ipvodTitle.html',selected='search',savelen=savelen, savelist=savelist,header =header)
    else:
        print(6)
        return render_template('ipvodTitle.html',selected='search',savelen=savelen, savelist=savelist,header =header)   

I believe the issue is here in the code

if re.search(catTitle,str(v['assetName'].lower())):

I've also tried

if catTitle.lower() in v['assetName'].lower():

but I get the same results. "show title" brings up all episodes and extras and "S*1*show title" brings up nothing

davidism
  • 121,510
  • 29
  • 395
  • 339
SM23
  • 25
  • 8

1 Answers1

0

Ok, to be honest, I've not really read your code, too much stuff here, but let's focus on what I think to be your actual question: How to use wildcard search with Python.

The only way I know is fnmatch:

import fnmatch

data = ['foo', 'foobar', 'foo-bar', 'foo-oo-bar', 'foo-foo', 'foo-oo-foo']

for item in data:
    print('{}: {}'.format(
        item,
        fnmatch.fnmatch(item, 'foo*bar')
    ))

Output:

foo: False
foobar: True
foo-bar: True
foo-oo-bar: True
foo-foo: False
foo-oo-foo: False

Please, not that wildcard is really not the same thing than Regex. Don't except to use wildcard via any Regex method.

Arount
  • 9,853
  • 1
  • 30
  • 43