将读取用户输入文件的函数放入并使用异常循环

2024-09-30 16:23:57 发布

您现在位置:Python中文网/ 问答频道 /正文

该程序正在读取一个关键字文件,其中附加了数值。然后,它正在读取一个包含数千条tweet的文件,其中包含纬度和经度以及tweet的文本。你必须将推文分类到特定区域,然后根据第一个文档的关键字和值计算每个区域的平均情绪。用户必须将这些输入到两个文件中,并且必须有一个带有异常错误的try语句。这些函数单独计算正确的值,但当我将其放入try语句时,会出现以下错误:

回溯(最近一次调用last):对于第129行main()和第16行sortKets(键)。最后一个错误行56关键字[lines[0]=int(lines[1])索引器:列表索引超出范围 我能帮你修一下吗

列表项

eastern = []
central = []
mountain = []
pacific = []
keyword = {}
easternsum =[]
centralsum= []
mountainsum = []
pacificsum = []
    def main() :
    done = False
    while not done:
        try:
            keys = input("Enter file: ")
            readkeys(keys)
            sortKeys(keys)

            tweets = input("Enter second file: ")
            readtweets(tweets)
            sorttweet(tweets)


            calcsentiment()
            print("The eastern amount of tweets is",len(easternsum))
            print("The eastern happiness score              is",sum(easternsum)/len(easternsum))
            print("The central amount of tweets is",len(centralsum))
        print("The central happiness score    is",sum(centralsum)/len(centralsum))
        print("The mountain amount of tweets is",len(mountainsum))
        print("The mountain happiness score is",sum(mountainsum)/len(mountainsum))
        print("The pacific amount of tweets is",len(pacificsum))
        print("The pacific happiness score is",sum(pacificsum)/len(pacificsum))
        done = True

    except IOError:
        print("Error, file not found.")

    except ValueError:
        print("Invalid file.")

    except RuntimeError as error:
        print("Error", str(error))



def readkeys(keys):
    keys = open(keys, "r")

def readtweets(tweets):
    tweets = open(tweets, "r")



def sortKeys(keys):
    for line in keys :
       lines = line.split(",")
       keyword[lines[0]] = int(lines[1])

def sorttweet(tweets) :
    for line in tweets :
        stuff = line.split(" ",5)
        long = float(stuff[0].strip("[,"))
        lat = float(stuff[1].strip('],'))
        tweet = stuff[5]
        if 24.660845 < long < 49.189787 and -87.518395 < lat < -67.444574 :
            eastern.append(tweet)
        if 24.660845 < long < 49.189787 and -101.998892 < lat < -87.518395 :
            central.append(tweet)
        if 24.660845 < long < 49.189787 and -115.236428 < lat < -101.998892 :
            mountain.append(tweet)
        if 24.660845 < long < 49.189787 and -125.242264 < lat < -115.236428 :
            pacific.append(tweet)




def calcsentiment():
    for tweet in eastern :
        tweetlist = tweet.split()
        count = 0
        tweetV = 0
        for word in tweetlist:
            if word in keyword :
                count = count + 1
                tweetV = tweetV + keyword[word]
        if count > 0:
            easternsum.append(tweetV / count)

for tweet in central:
    tweetlist2 = tweet.split()
    count = 0
    tweetV = 0
    for word in tweetlist2 :
        if word in keyword :
            count = count + 1
            tweetV = tweetV + keyword[word]
    if count > 0:
        centralsum.append(tweetV / count)


for tweet in mountain:
    tweetlist3 = tweet.split()
    count = 0
    tweetV = 0
    for word in tweetlist3 :
        if word in keyword :
            count = count + 1
            tweetV = tweetV + keyword[word]
    if count > 0:
        mountainsum.append(tweetV / count)

for tweet in pacific:
    tweetlist4 = tweet.split()
    count = 0
    tweetV = 0
    for word in tweetlist4 :
        if word in keyword :
            count = count + 1
            tweetV = tweetV + keyword[word]
    if count > 0:
        pacificsum.append(tweetV / count)
calcsentiment()







main()

Tags: theinforlenifiscountkeys
1条回答
网友
1楼 · 发布于 2024-09-30 16:23:57

您在这里遇到了一个问题:

def sortKeys(keys): 
    for line in keys : 
        lines = line.split(",") 
        keyword[lines[0]] = int(lines[1])

当你分线时,你不会得到2个代币,只有一个。 当尝试拆分的行不包含“,”字符时,会发生这种情况。 在python控制台中尝试某个行“xxxx”.split(“,”),您将看到结果是[“xxxx”],因此只有一个元素的列表,而在代码行[1]中尝试访问列表的第二个元素

相关问题 更多 >