简体   繁体   English

UnboundLocalError:分配前已引用局部变量“ Counter”

[英]UnboundLocalError: local variable 'Counter' referenced before assignment

Here is my code 这是我的代码

    #word count2
from collections import Counter
def main():
    with open("search words.txt","r") as myfile:
         data=myfile.read()
         topTenWords(data)


def topTenWords(wordCountDict):
    split_it=wordCountDict.split()
    Counter = Counter(split_it)
    most_common=Counter.most_common(10)
    print(most_common)

if __name__=='__main__':
  main()

then upon running the above code I am getting error 然后在运行上面的代码时我得到了错误

word count2.py 
Traceback (most recent call last):
  File "D:/word count2.py", line 16, in <module>
    main()
  File "D:/word count2.py", line 6, in main
    topTenWords(data)
  File "D:/word count2.py", line 11, in topTenWords
    Counter = Counter(split_it)
UnboundLocalError: local variable 'Counter' referenced before assignment

What is the mistake in above code? 上面的代码有什么错误?

You are overwriting the imported Counter Class with a variable. 您正在用变量覆盖导入的Counter类。 Just write for example counter = Counter(split_it) and it should work. 只需编写例如counter = Counter(split_it) ,它应该可以工作。

Also just btw, you might wanna read the PEP8 Style Guide for Python, usually you don't use variable names that start with capital letters. 顺便说一句,您可能想阅读《 PEP8 Python样式指南》,通常您不使用以大写字母开头的变量名称。

this should works: 这应该工作:

# coding:utf-8
from collections import Counter


def main():
    with open("../docs/words.txt", "r") as myfile:
        data = myfile.read()
        topTenWords(data)
def topTenWords(wordCountDict):
    split_it = wordCountDict.split()
    counter = Counter(split_it)
    most_common = counter.most_common(10)
    print(most_common)


if __name__ == '__main__':
    main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UnboundLocalError:分配前已引用局部变量“ truebomb” - UnboundLocalError: local variable 'truebomb' referenced before assignment UnboundLocalError:分配前已引用局部变量“ cur” - UnboundLocalError: local variable 'cur' referenced before assignment UnBoundLocalError:赋值之前引用的局部变量(Python) - UnBoundLocalError: local variable referenced before assignment (Python) UnboundLocalError:分配前已引用局部变量“ strdate” - UnboundLocalError: local variable 'strdate' referenced before assignment UnboundLocalError:赋值之前引用了局部变量“ key” - UnboundLocalError: local variable 'key' referenced before assignment unboundLocalError:赋值前引用了局部变量“loopback” - unboundLocalError: local variable 'loopback' referenced before assignment UnboundLocalError:分配前已引用局部变量“ endProgram” - UnboundLocalError: local variable 'endProgram' referenced before assignment UnboundLocalError:赋值前引用了局部变量“Score” - UnboundLocalError: local variable 'Score' referenced before assignment UnboundLocalError:分配前引用了局部变量“检查” - UnboundLocalError: local variable 'checking' referenced before assignment UnboundLocalError:在赋值之前引用了局部变量“区域” - UnboundLocalError: local variable 'region' referenced before assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM