2017-09-01 106 views
-1

我在Python 3.5.1上使用了熊猫库。我如何从字段值中删除html标签?这里是我的输入和输出:删除熊猫中的html标签

enter image description here

我的代码返回了一个错误:

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) 
+0

'df'的定义会产生以下错误:'ValueError:传递值的形状是(1,3),索引暗示(1,2)'。 – GLR

+0

你说得对。我修正了这一点。 –

回答

0

像这样re.sub('<[^<]+?>', '', text)

你可以找到详细内容解答there