none
Why doesn't my program display (black screen and no error either) when I run my double for loop using pyodbc in a Python app ? RRS feed

  • Question

  • Hi, 

    I've made a script that runs perfectly on a simple list on Visual Studio 2017. But since I've been trying to run it on an Access database, the first two parts of the code work fine when separated from the last part, and when I run it with the last part containing my double for loop, nothing displays. I just have a black window and no error can be found anywhere. Yesterday I had a StopIteration error (location shown in the sample above), but since I tried with a simple for loop instead of a list comprehension there's absolutely no error appearing. 

    Here's my code : 

    # -*- coding: utf-8 -*-
    import pyodbc
    import re 
    from difflib import SequenceMatcher
    
    [x for x in pyodbc.drivers() if x.startswith('Microsoft Access Driver')]
    
    # Connection to accdb
    
    conn_str = (
        r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'
        r'DBQ=C:\\Users\\alice\\Desktop\\lexique3.accdb;'
        )
    cnxn = pyodbc.connect(conn_str)
    crsr = cnxn.cursor()
    
    # Put all words into a list 
    
    crsr.execute("select unites_lexicales from Mot where cat_grammat='Verbe' or 'Nom' or 'Adjectif' or 'Adverbe'")
    resultat = crsr.fetchall()
    words = [row[0] for row in resultat]
    #print(words)
    
    # Remove unwanted characters and put radicals into a list
    
    radicals = [] 
    motifp = "^(an|re|dé|dés)" 
    motifs = "(ist|man|er|an|able|asyon)$" 
    
    for word in words : 
        word = re.sub(motifp, '', word) 
        word = re.sub(motifs, '', word) 
        radicals.append(word) 
    #print(radicals) 
    
    # Match radicals and put words into new lists
    
    ratio = 0.6
    n = len(radicals)
    result = []
    used_js = []
    
    for i in range(n) :
        if i in used_js:
            continue
        matches = [words[i]]
        js = (x for x in range(n) if x != i and x not in used_js)
        for j in js : # Previous error : StopIteration
            if SequenceMatcher(None, radicals[i], radicals[j]).ratio() >= ratio : 
                matches.append(words[j])
                used_js.append(j)
        result.append(matches)
    print(result)

    Here's the expecting result (which I previously had by running the code on a simple list) : 

    ['flore', 'fleur', 'fleuriste'], ['remaniement', 'remanier', 'manier', 'maniable'], ['désaimer', 'aimer', 'aimant'], ['mêler', 'emmêler', 'désemmêler']
    I guess it comes from the double for loop, but I really can't figure out why since I tested it before.
    Any clue about why I can't run it and how I can fix this would be really helpful. Thanks a lot !



    • Edited by Alice Phoenix Saturday, October 14, 2017 10:22 AM
    • Moved by 宝宝徐 Friday, October 20, 2017 1:59 AM
    Saturday, October 14, 2017 10:20 AM

All replies

  • I think that the first part requires these adjustments:

    crsr.execute("select unites_lexicales from Mot where cat_grammat IN ('Verbe', 'Nom', 'Adjectif', 'Adverbe')")
    . . .
    motifp = "^(an|re|dés|dé)"
    
    

    Then check if words and radicals contain the expected values.

    Maybe the screen is blank because the list of words is too large and it takes time to obtain the results. How many words do you have?

    • Edited by Viorel_MVP Saturday, October 14, 2017 6:12 PM
    • Proposed as answer by Fletch Zhou Wednesday, October 18, 2017 9:41 AM
    Saturday, October 14, 2017 6:11 PM