简体   繁体   English

AttributeError: 'generator' object 没有属性 'isdigit'

[英]AttributeError: 'generator' object has no attribute 'isdigit'

I'm trying to go through this file that I've created and only extract the ages of the people in the file.我正在尝试通过我创建的这个文件来 go 并且只提取文件中人员的年龄。 then take the average of the ages and print it out.然后取年龄的平均值并将其打印出来。 I'm trying to iterate through the lines in the file and it says that the generator object has no attribute.我试图遍历文件中的行,它说生成器 object 没有属性。 I'm using python 3 and I commented out some code I was using before so I don't have to worry about it.我正在使用 python 3 并且我注释掉了我之前使用的一些代码,所以我不必担心它。 I don't know if I should use the commented code either but I'm just scared of losing all the ideas I've gotten this far.我也不知道我是否应该使用带注释的代码,但我只是害怕失去到目前为止我得到的所有想法。 How do I get those ages in my loop if I can't iterate through?如果我无法迭代,如何在循环中获取这些年龄?

 f = open('myfile.txt','w') f.write('Name, Age, Gender, Profession\n') f.write('Harry, 23, Male, Software Engineer\n') f.write('Sam, 25, Male, Lawyer\n') f.write('Lisa, 29, Female, Computer Scientist\n') f.write('Mary, 22, Female, Doctor\n') f.close() # print(readFile.read()) # readFile = open("myfile.txt") with open('myfile.txt', 'r') as file: for line in file: values = (line.split(', ') for line in file) if values.isdigit[1] == True: numbers = 0 numbers += values average = numbers % 4 print(average)

Try:尝试:

f = open('myfile.txt','w')
f.write('Name, Age, Gender, Profession\n')
f.write('Harry, 23, Male, Software Engineer\n')
f.write('Sam, 25, Male, Lawyer\n')
f.write('Lisa, 29, Female, Computer Scientist\n')
f.write('Mary, 22, Female, Doctor\n')
f.close()

with open('myfile.txt', 'r') as file:
    numbers = 0
    for line in file:
        values = line.split(', ')
        if values[1].isdigit() == True:
            numbers += int(values[1])

    average = numbers / 4
    print(average) # 24.75

There are several issues:有几个问题:

  1. values = (line.split(', ') for line in file) assignes a generator to values , and a generator does not have isdigit . values = (line.split(', ') for line in file)将生成器分配给values ,而生成器没有isdigit You are already using a for loop, so you don't need a generator (actually you shouldn't).您已经在使用for循环,因此您不需要生成器(实际上您不应该)。

  2. In the corrected code, values now is a list of strings.在更正后的代码中, values现在是字符串列表。 So you need to use values[1] to access an item of the list, and then apply isdigit .因此,您需要使用values[1]访问列表中的一项,然后应用isdigit

  3. You are resetting numbers in each iteration.您在每次迭代中重置numbers Initiate the variable before the loop.在循环之前启动变量。

  4. values[1] is a string, so in order to add this to numbers , you need int . values[1]是一个字符串,因此要将其添加到numbers ,您需要int

  5. % is the modulus operator. %是模数运算符。 Use / for division.使用/进行除法。

  6. Using a hard-coded number (in this case, dividing something by a specific number 4 ) is not a best option.使用硬编码的数字(在这种情况下,将某物除以特定数字4 )不是最佳选择。 You might want an alternative way to get the number of records.您可能需要另一种方法来获取记录数。

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

相关问题 AttributeError:'int'对象没有属性'isdigit' - AttributeError: 'int' object has no attribute 'isdigit' AttributeError: 'int' 对象没有属性 'isdigit' 和 NameError - AttributeError: 'int' object has no attribute 'isdigit' and NameError Python-AttributeError:“ str”对象没有属性“ isDigit” - Python - AttributeError: 'str' object has no attribute 'isDigit' Python AttributeError:“系列”对象没有属性“ isdigit” - Python AttributeError: 'Series' object has no attribute 'isdigit' AttributeError:“列表”对象没有属性“ isdigit” - AttributeError: 'list' object has no attribute 'isdigit' AttributeError: 'int' object 没有属性 'isdigit' 和 arrays - AttributeError: 'int' object has no attribute 'isdigit' with arrays 为什么我的代码不起作用 AttributeError: 'int' object has no attribute 'isdigit' - why my code not working AttributeError: 'int' object has no attribute 'isdigit' AttributeError:'int'对象没有用户输入的属性'isdigit' - AttributeError: 'int' object has no attribute 'isdigit' from user input AttributeError'int'对象在Ubuntu中的ipython中没有属性isdigit - AttributeError 'int' object has no attribute isdigit in ipython in ubuntu AttributeError:生成器对象没有属性“ sort” - AttributeError: generator object has no attribute 'sort'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM