python-2.7 - 属性错误: 'NoneType' object has no attribute 'encode' in python 2. 7

标签 python-2.7

for item in root.findall('./channel/item'):
    news = {}
    # iterate child elements of item
    for child in item:
        # special checking for namespace object content:media
        if child.tag == '{http://search.yahoo.com/mrss/}content':
            news['media'] = child.attrib['url']
        else:
            news[child.tag] = child.text.encode('utf8')
    newsitems.append(news)

有什么问题吗?我该如何解决这个问题?

最佳答案

这里:

    else:
        news[child.tag] = child.text.encode('utf8')
在某些情况下,

child.textNone。因此,在这种情况下不要创建字典条目,例如这样:

    elif child.text is not None:
        news[child.tag] = child.text.encode('utf8')

关于python-2.7 - 属性错误: 'NoneType' object has no attribute 'encode' in python 2. 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45322223/

相关文章:

python - 给定一个字符串,波兰表示法中最长的 WFF 之一的长度是多少?

python-2.7 - 在Python中的数据流中写入云存储的动态目标

python - 如何在正确的 python 版本中安装 numpy 和 matplotlib?

python-2.7 - Scikit-Learn One-hot-encode 在训练/测试拆分之前或之后

python - 使用 PyInstaller 或 cx_Freeze 使用 Anaconda2 生成非常大的文件

python - matplotlib 绘制条形图和折线图在一起

python - 打印/输出时如何从列表中删除方括号

python - 在 Windows 上的 python 中获取 CPU 温度

Python 与 Theano 缺少模块

python - 为什么 json.loads 关心使用哪种类型的引号?