我的数据框看起来像-
id text
1 ><body lang="EN-IN" link="#0563C1" vlink="#954F72"><div class="WordSection1"><p
class="MsoNormal">I am interested.</p><p class="MsoNormal">I need :</p><p
class="MsoNormal">330 wp</p><p class="MsoNormal">50 modules</p><p
class="MsoNormal"> </p><table class="MsoNormalTable" border="0"
cellspacing="0" cellpadding="0" width="0" style="width:282.0pt; border-
collapse:collapse">
2 ><body lang="EN-IN" link="#0563C1" vlink="#954F72"><div class="WordSection1"><p
class="MsoNormal">I am not interested.</p><p
class="MsoNormal"> </p><table class="MsoNormalTable" border="0"
cellspacing="0" cellpadding="0" width="0" style="width:282.0pt; border-
collapse:collapse">
我的最终数据框看起来像-
id text final_text
1 ><body lang="EN-IN" link="#0563C1" I am interested. I need
vlink="#954F72"><div class="WordSection1"><p 330 wp 50 modules
class="MsoNormal">I am interested.</p>
<p class="MsoNormal">I need :</p><p
class="MsoNormal">330 wp</p><p
class="MsoNormal">50 modules</p><p
class="MsoNormal"> </p><table
class="MsoNormalTable" border="0"
cellspacing="0" cellpadding="0"
width="0" style="width:282.0pt; border-
collapse:collapse">
2 ><body lang="EN-IN" link="#0563C1" I am not interested.
vlink="#954F72"><div class="WordSection1"><p
class="MsoNormal">I am not interested.</p><p
class="MsoNormal"> </p><table
class="MsoNormalTable" border="0"
cellspacing="0" cellpadding="0"
width="0" style="width:282.0pt; border-
collapse:collapse">
逻辑应捕获<p class="MsoNormal"> and </p>.
之间存在的所有信息,但不包括此字符串" "
。我想在python中做。
答案 0 :(得分:0)
通常,人们不会发布诸如“为此给我代码”之类的问题。您可以尝试发布与“如何从看起来像这样的句子中删除标签”之类的问题。
无论如何,这是您的解决方案。
def get_sent(s):
sent = ' '.join([i[1:] for i in s[1:].replace('>','|~').replace('<','|').split('|') if i[:1]=='~' and len(i)>1 and i[:2]!='~&'])
return sent
df['final_text'] = df['text'].apply(get_sent)
希望这会有所帮助。