为什么我的代码可以正常工作但可以无限打印?

时间:2019-11-26 17:43:29

标签: python-3.x function csv

我正在下载两个excel电子表格,一个是顶级域名(TLD)及其定义的列表,另一个是前一百万个网站的列表。我的部分功能是根据用户输入来查找特定TLD的顶部位置。代码“有效”,但是它无限地打印等级/关键字,我不知道为什么,希望能得到任何帮助。

def parseCSV(tlds, top):

    tldDict = {
        " ":" "
    }

    topDict = {
        " ":" "
    }

    #opening CSV file using string from parameter
    file = open(r"C:\Users\bubba\OneDrive\Documents\Foundation Programming\tlds.csv", 'r')

    file2 = open(r"C:\Users\bubba\OneDrive\Documents\Foundation Programming\top 1m.csv", 'r')

    #iterating through file and splitting each line by comma
    #this is put into a dictionary
    for i in file.readlines():
        x=i.split(",")
        tldDict[x[0]]=x[1]
    file.close()

    for i in file2.readlines():
        x=i.split(",")
        topDict[x[0]]=x[1]
    file2.close()

    return [tldDict,topDict]

def getTopTLD(tld, tldDict, topDict):
    matched_keys = []
    for key, pair in topDict.items():
        if tld in pair:
            matched_keys.append(key) # Simple append statement 
            print('The position of this tld is:', min(matched_keys))
    return matched_keys

tldDict, topDict = parseCSV("tlds","top-1m")

while True:
    tld = input("Enter tld ")

    if tld == "exit":
        break

    getTopTLD(tld, tldDict, topDict)

控制台示例:

Enter tld com
The position of this tld is: 1
The position of this tld is: 1
The position of this tld is: 1
The position of this tld is: 1
The position of this tld is: 1

我认为这可能是while循环,但是如果删除它,结果是相同的。

0 个答案:

没有答案