如何使用Python从文本文件中剥离SGML标记?

2024-09-28 10:14:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近碰到了Standard Generalized Markup Language。我从EMILLE/CIIL Corpus获得了SGML格式的语料库。以下是该语料库的文档:

EMILLE Corpus Documentation

我只想提取文件中的文本。文档中语料库的编码和标记信息为:

The text is encoded as two-byte Unicode text. For more information on Unicode. The texts are marked up in SGML using level 1 CES-compliant markup. Each file also includes a full header, which specifies the provenance of the text.

我很难脱掉这些标签。我试过用“正则表达式”和“靓汤”但都不管用。这是示例文本文件。我想保留的语言是旁遮普语。在

Sample text file


Tags: thetext文档格式unicodecorpuslanguagestandard
2条回答

尝试以下操作:

from bs4 import BeautifulSoup
import requests

# Assuming this is the url where the file is
html = requests.get('http://www.lancaster.ac.uk/fass/projects/corpus/emille/MANUAL.htm').content

bsObj = BeautifulSoup(html)

textData = bsObj.findAll('p')

for item in textData:
    print item.get_text()

或者 您可以使用简单的正则表达式;如果data是包含以<;开头、以gt;结尾的标记的字符串,则这些标记之间的所有内容都将被丢弃—您可以将多个空白限制为一个,并删除数据。在

data = re.sub(r'<.*?>', '', data)
data = re.sub(r'\s+', ' ', data)
data = data.strip()

相关问题 更多 >

    热门问题