Python 获取错误'_io.TextIOWrapper';对象没有属性';ascii#u lower';

Python 获取错误'_io.TextIOWrapper';对象没有属性';ascii#u lower';,python,python-3.x,Python,Python 3.x,这是我在运行代码时得到的回溯: def get_frequencies(filename: str) -> None: ''' 1. Open a text file. 2. Read all its lines. 3. Turn each line to lower case (use .lower() ) 4. Ignore any letters that are not a-z (use string.ascii_lower) 5.

这是我在运行代码时得到的回溯:

def get_frequencies(filename: str) -> None:
    '''
    1. Open a text file.
    2. Read all its lines.
    3. Turn each line to lower case (use .lower() )
    4. Ignore any letters that are not a-z (use string.ascii_lower)
    5. Compute the counts for each letter
    6. Go through all the letters,
    6. Compute the frequency of each letter.
    7. Print the letter, and its frequency.

    Parameter:
    ---------
    filename: the name of the text file
    '''

    text = open(filename, 'r')
    letters = text.ascii_lower
    for i in text:
      text_lower = i.lower()
      text_nospace = text_lower.replace(" ", "")
      text_nopunctuation = text_nospace.strip(string.punctuation)
      for a in letters:
          if a in text_nopunctuation:
              num = text_nopunctuation.count(a)
              print(a, num)
回溯(最近一次呼叫最后一次):
文件“C:/MyPython/Code/StackOverflow Questions/get_-breake.py”,第28行,在
获取频率('test1.txt')
文件“C:/MyPython/Code/StackOverflow Questions/get\u frequencies-break.py”,第18行,在get\u frequencies中
字母=text.ascii\u更低
AttributeError:“\u io.TextIOWrapper”对象没有属性“ascii\u lower”

您需要导入字符串,并且需要为字母变量指定string.ascii_小写字母

以下是一个工作版本:

Traceback (most recent call last):
  File "C:/MyPython/Code/StackOverflow-Questions/get_frequencies-Broken.py", line 28, in <module>
    get_frequencies('test1.txt')
  File "C:/MyPython/Code/StackOverflow-Questions/get_frequencies-Broken.py", line 18, in get_frequencies
    letters = text.ascii_lower
AttributeError: '_io.TextIOWrapper' object has no attribute 'ascii_lower'

虽然可以就家庭作业提出问题,但至少应该有一次尝试解决任务和一个具体的问题。至少问题的格式应该很好。您可以在这里找到帮助:错误有哪些地方不清楚
text
是一个文件对象,它没有
ascii\u lower
属性。您的意思可能是
string.ascii\u lower
。也许如果你能分享一些背景知识,并提出一个问题,我们就能帮助你。。。
import string

def get_frequencies(filename: str) -> None:
    '''
    1. Open a text file.
    2. Read all its lines.
    3. Turn each line to lower case (use .lower() )
    4. Ignore any letters that are not a-z (use string.ascii_lower)
    5. Compute the counts for each letter
    6. Go through all the letters,
    6. Compute the frequency of each letter.
    7. Print the letter, and its frequency.

    Parameter:
    ---------
    filename: the name of the text file
    '''
    text = open(filename, 'r')
    letters = string.ascii_lowercase
    for i in text:
      text_lower = i.lower()
      text_nospace = text_lower.replace(" ", "")
      text_nopunctuation = text_nospace.strip(string.punctuation)
      for a in letters:
          if a in text_nopunctuation:
              num = text_nopunctuation.count(a)
              print(a, num)