python - 在 Python 中使用解析器时出现类型错误

标签 python

我有以下 html 解析器:

from HTMLParser import HTMLParser

class MLStripper(HTMLParser):
    def __init__(self):
        self.reset()
        self.fed = []

    def handle_data(self, d):
        self.fed.append(d)

    def get_data(self):
        return ''.join(self.fed)

def strip_tags(html):
    s = MLStripper()
    s.feed(html)
    return s.get_data()

我想在以下 data.frame 上使用它:

 df = pd.DataFrame([['<br> test </br>', 1]], columns=('body', 'ticketID'))

我的假设是它会像这样工作:

 for row in df.iterrows():
     input = row['body']
     print(strip_tags(input)

但这给了我一个类型错误。有什么想法哪里出了问题吗?

最佳答案

来自 ( Docs ):

DataFrame.iterrows()

Iterate over DataFrame rows as (index, Series) pairs.

这样你就得到了索引和行。

工作代码:

for index, row in df.iterrows():
    input = row['body']
    print(strip_tags(input))

关于python - 在 Python 中使用解析器时出现类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41853354/

相关文章:

python - pd.qcut 返回负值

python - 为什么当 Python venvs 也有 python.exe 文件时,它们的配置中有 "home"设置?

python - 如何在 Flask 中创建忽略静态文件的包罗万象的 URL 路由?

python - 使用 render_to_string 时填充 {% csrf_token %}?

python - 在字典中循环键

python - 如何强制 virtualenv 从 pypi 安装最新的 setuptools 和 pip?

python - 在 sympy 中设置 Latex 打印的全局设置

python - 使用 Flask 应用程序时,使用 print 会对性能产生负面影响吗?

python - 按所有元组中的两个元素对元组列表进行排序

python - 如何在 Keras 中将密集层归零?