2017-08-04 53 views
0

我有一些记录看起来像这样的一个DF:清洁CSS样式块从熊猫数据框中

Untitledp { margin-top: 0px;margin-bottom: 0px;line-height: 1.15; } body { font-family: 'Times New Roman';font-style: Normal;font-weight: normal;font-size: 13.3333333333333px; } .Normal { telerik-style-type: paragraph;telerik-style-name: Normal;border-collapse: collapse; } .TableNormal { telerik-style-type: table;telerik-style-name: TableNormal;border-collapse: collapse; } .s_F0039783 { telerik-style-type: local;font-size: 13.34px; } .s_45EBF2E0 { telerik-style-type: local;font-family: 'Times New Roman';font-size: 13.3333333333333px;color: #000000; } A sentence that I actually want. 

我想删除的CSS样式块,只在最后返回的句子。每个记录的CSS块数量可能不同。所有记录都以“Untitledp”开头,并以我想要的文本结尾(文本后没有样式块)。

我应该如何清洁这些块?我使用BeautifulSoup来清理html标签,但它不适用于这些块。

回答

1

甲正则表达式可用于这一点,与sub()

regex = re.compile('.+\s*{.*}') 
regex.sub('', s) # s is copy paste of your sample 
' A sentence that I actually want.' 

至少它的工作原理在本实施例中。但要小心,如果您想要获得的句子中有{},这将会失败。但是,由于句子通常不包含这些字符...