2017-09-01 107 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