【发布时间】:2018-02-10 11:17:04
【问题描述】:
我在 Python 3.5.1 上使用 pandas 库。如何从字段值中删除 html 标签?这是我的输入和输出:
我的代码返回错误:
import pandas as pd
code=[1,2,3]
overview =['<p>Environments subject.</p>',
'<ul><li> property ;</li></ul><ul><li>markets and exchange;</li></ul>',
'<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">']
# '<p class="SSPBodyText" style="padding: 0cm; text-align: justify;">The subject.</p>']
df= pd.DataFrame(overview,code)
df.columns = ['overview']
df['overview_copy'] = df['overview']
# print(df)
tags_list = ['<p>' ,'</p>' , '<p*>',
'<ul>','</ul>',
'<li>','</li>',
'<br>',
'<strong>','</strong>',
'<span*>','</span>',
'<a href*>','</a>',
'<em>','</em>']
for tag in tags_list:
# df['overview_copy'] = df['overview_copy'].str.replace(tag, '')
df['overview_copy'].replace(to_replace=tag, value='', regex=True, inplace=True)
print(df)
【问题讨论】:
-
df的定义抛出如下错误:ValueError: Shape of passed values is (1, 3), indices imply (1, 2)。 -
你是对的。我修好了。
标签: python html python-3.x regex pandas