简体   繁体   English

python“'NoneType'对象没有属性'encode'”

[英]python “'NoneType' object has no attribute 'encode'”

I wrote this tiny Python snippet that scrapes a feed and prints it out. 我写了这个微小的Python片段,它抓取一个feed并将其打印出来。 When I run the code, something in the feed triggers the error message you see here as my question. 当我运行代码时,Feed中的某些内容会触发您在此处看到的错误消息作为我的问题。 Here's the complete console output on error: 这是错误的完整控制台输出:

> Traceback (most recent call last):  
> File "/home/vijay/ffour/ffour5.py",
> line 20, in <module>
>     myfeed()   File "/home/vijay/ffour/ffour5.py", line
> 15, in myfeed
>     sys.stdout.write(entry["title"]).encode('utf-8')
> AttributeError: 'NoneType' object has
> no attribute 'encode'
 > sys.stdout.write(entry["title"]).encode('utf-8') 

This is the culprit. 这是罪魁祸首。 You probably mean: 你可能意味着:

sys.stdout.write(entry["title"].encode('utf-8'))

(Notice the position of the last closing bracket.) (注意最后一个右括号的位置。)

Lets try to clear up some of the confusion in the exception message. 让我们尝试清除异常消息中的一些混淆。

The function call 函数调用

sys.stdout.write(entry["title"])

Returns None. 返回无。 The ".encode('utf-8')" is a call to the encode function on what is returned by the above function. “.encode('utf-8')”是对上述函数返回的编码函数的调用。

The problem is that None doesn't have an encode function (or an encode attribute) and so you get an attribute error that names the type you were trying to get an attribute of and the attribute you were trying to get. 问题是None没有编码函数(或编码属性),因此您得到一个属性错误,该错误指出了您尝试获取属性的类型以及您尝试获取的属性。

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

相关问题 &#39;NoneType&#39;对象没有属性&#39;encode&#39; - 'NoneType' object has no attribute 'encode' 如何解决nonetype对象在python 2.7中没有属性编码错误 - How to resolve nonetype object has no attribute encode error in python 2.7 如何解决AttributeError:&#39;NoneType&#39;对象在python中没有属性&#39;encode&#39; - How to resolve AttributeError: 'NoneType' object has no attribute 'encode' in python Cassandra 3.7 with Python,&#39;NoneType&#39; 对象没有属性 &#39;encode_message&#39; - Cassandra 3.7 with Python, 'NoneType' object has no attribute 'encode_message' AttributeError:“ NoneType”对象没有带有lxml-python的属性“ encode” - AttributeError: 'NoneType' object has no attribute 'encode' with lxml-python NoneType 对象没有属性“编码”(Web Scraping) - NoneType object has no attribute 'encode' (Web Scraping) AttributeError:“ NoneType”对象没有属性“ encode” - AttributeError: 'NoneType' object has no attribute 'encode' pymysql问题&#39;NoneType&#39;对象没有属性&#39;encode&#39; - pymysql issue 'NoneType' object has no attribute 'encode' AttributeError 'NoneType' object 没有属性 'encode' - AttributeError 'NoneType' object has no attribute 'encode' Python&#39;NoneType&#39;对象没有属性&#39;...&#39; - Python 'NoneType' object has no attribute '…'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM