当我在Python应用程序中使用pyodbc运行双循环时,为什么我的程序不显示(黑屏也没有错误)? [英] 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 ?

查看:52
本文介绍了当我在Python应用程序中使用pyodbc运行双循环时,为什么我的程序不显示(黑屏也没有错误)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 
$


我制作了一个在Visual Studio 2017上的简单列表上完美运行的脚本。但是因为我已经试图在Access数据库上运行它,代码的前两部分在与最后一部分分离时工作正常,当我运行它时,最后一部分包含
我的double for循环,没有任何显示。我只有一个黑色的窗口,在任何地方都找不到任何错误。昨天我遇到了StopIteration错误(上面示例中显示的位置),但由于我尝试使用简单的for循环而不是列表理解,因此
绝对没有出现错误。 



这是我的代码: 

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 !

推荐答案

我认为第一部分需要进行这些调整:

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.

可能屏幕空白因为单词列表太大而且需要时间来获得结果。你有几个字?


这篇关于当我在Python应用程序中使用pyodbc运行双循环时,为什么我的程序不显示(黑屏也没有错误)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆