【问题标题】:'module' object is not callable (Python3)“模块”对象不可调用(Python3)
【发布时间】:2022-01-09 00:39:59
【问题描述】:

我一直在尝试创建此代码来分析文本中的单词,并根据年份对这些单词在文本中出现的次数进行分类。 创建这样的代码后:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import morphemes as mf
import index_terms as idt

###############################################################################
def count_yr(counter, filename, index, size):
    '''
    counter: dictionary (key: word, value: frequency list)
    filename : file for analyzing words
    index : index for year 2000->0, 2001->1, ...
    size : total number of years
    '''
    word_count = {}
    with open(filename, "r") as file:
        lst = idt(mf(file))
        for word in lst:
            counter[word] = [0]*size
        for word in lst:
            if word in counter:
                counter[word][index] += 1
            else:
                counter[word][index] = 1
    return counter







###############################################################################
if __name__ == "__main__":

    if len(sys.argv) < 2:
        print( "[Usage]", sys.argv[0], "in-file(s)", file=sys.stderr)
        sys.exit()

    counter = {}

    for i, filename in enumerate(sys.argv[1:]):
        count_year( counter, filename, i, len(sys.argv[1:]))

    while True:
        query = input('Please type the word you are looking for (type "exit" to exit): ')

        if query == "exit":
            break
        
        if query in counter:
            print(counter[query])
        else:
            print("No Result")

好像弹出了"TypeError: 'module' object is not callable"。 我创建了 morphemes.py 和 index_terms.py,它们运行良好。 我不知道如何解决它。谁能帮帮我?

【问题讨论】:

  • 您希望调用 模块 index_termsmorphemes 做什么?你的意思是from index_terms import idtfrom morphemes import mf 吗?
  • 是的,我把它误认为是别的东西了!感谢您指出它们!

标签: python linux file ubuntu module


【解决方案1】:

在不知道 morphemes.py 和 index_terms.py 的结构的情况下,我假设您需要从这些模块中导入一些可调用对象,而不是尝试调用模块本身,在 lst = idt(mf(file)) 行。

from morphemes import SomeCallableYouNeed as mf
from index_terms import SomeOtherCallableYouNeed as idt

【讨论】:

  • 啊,非常感谢!我发现了问题!
猜你喜欢
  • 1970-01-01
  • 2021-02-15
  • 2017-06-17
  • 2012-09-28
  • 2014-09-21
  • 2021-12-26
  • 2011-05-30
相关资源
最近更新 更多