简体   繁体   English

Python:TypeError:'NoneType' 类型的对象没有 len()

[英]Python: TypeError: object of type 'NoneType' has no len()

I am getting an error from this Python code:我从这段 Python 代码中收到错误:

with open('names') as f:
    names = f.read()
    names = names.split('\n')
    names.pop(len(names) - 1)
    names = shuffle(names)
    f.close()

assert len(names) > 100

Error:错误:

Python: TypeError: object of type 'NoneType' has no len()

The assert statement is throwing this error, what am I doing wrong?断言语句抛出这个错误,我做错了什么?

shuffle(names) is an in-place operation. shuffle(names)是就地操作。 Drop the assignment.放下作业。

This function returns None and that's why you have the error:此函数返回None ,这就是您出现错误的原因:

TypeError: object of type 'NoneType' has no len()

You don't need to assign names to list or [] or anything else until you wish to use it.在您希望使用它之前,您不需要将names分配给list[]或其他任何东西。

It's neater to use a list comprehension to make the list of names.使用列表理解来制作名称列表会更整洁。

shuffle modifies the list you pass to it. shuffle修改您传递给它的列表。 It always returns None它总是返回None

If you are using a context manager ( with... ) you don't need to close the file explicitly如果您使用的是上下文管理器( with... ),则不需要显式关闭文件

from random import shuffle

with open('names') as f:
    names = [name.rstrip() for name in f if not name.isspace()]
    shuffle(names)

assert len(names) > 100

What is the purpose of this这样做的目的是什么

 names = list;

? Also, no ;另外,不; required in Python.在 Python 中是必需的。

Do you want你想要

 names = []

or要么

 names = list()

at the start of your program instead?在你的程序开始而不是? Though given your particular code, there's no need for this statement to create this names variable since you do so later when you read data into it from your file.尽管给定了您的特定代码,但此语句不需要创建此names变量,因为您稍后会在从文件中读取数据时这样做。

@JBernardo has already pointed out the other (and more major) problem with the code. @JBernardo 已经指出了代码的另一个(也是更主要的)问题。

I was faces this issue but after change object into str, problem solved.我遇到了这个问题,但是在将对象更改为 str 之后,问题就解决了。 str(fname).isalpha(): str(fname).isalpha():

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

相关问题 Python-TypeError:“ NoneType”类型的对象没有len() - Python - TypeError: object of type 'NoneType' has no len() Python TypeError:“NoneType”类型的对象没有 len() - Python TypeError: object of type 'NoneType' has no len() Python TypeError: (“'NoneType' 类型的对象没有 len()” - Python TypeError: (“object of type 'NoneType' has no len()” TypeError:类型为'NoneType'的对象没有len()python - TypeError: object of type 'NoneType' has no len() python Python错误“ TypeError:类型为'NoneType'的对象没有len() - Python error "TypeError: object of type 'NoneType' has no len() Python 错误 - TypeError:“NoneType”类型的 object 没有 len() - Python error - TypeError: object of type 'NoneType' has no len() Python:TypeError:“NoneType”类型的 object 没有 len():使用 JSON 和 rncryptor - Python: TypeError: object of type 'NoneType' has no len(): Using JSON And rncryptor TypeError:“NoneType”类型的对象在应用程序部署中没有 len() Python - TypeError: object of type 'NoneType' has no len() Python on application deployment Python tkinter TypeError:类型为“ NoneType”的对象没有len() - Python tkinter TypeError: object of type 'NoneType' has no len() Python 错误:“TypeError:“NoneType”类型的 Object 没有 len()” - Python error: “TypeError: Object of type 'NoneType' has no len()”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM