跳转到主要内容

Python扩展,用于从字符串中删除标记。

项目描述

软件包haufe.stripml

Python扩展,用于从文本中删除HTML标记。

此软件包简单地以非常快速和残酷的方式从文本中删除类似HTML的标记。它很容易被用来从文本中删除XML或SGML。它不进行任何语法检查。

核心功能是用C++编程语言实现的,因此比使用SGMLParser或正则表达式进行相同任务要快得多。

许可证

本软件包采用LGPL 3许可证发布,请参阅LICENSE.txt。

安装

使用easy_install

easy_install haufe.stripml

测试

可以通过输入以下命令来测试haufe.stripml:

python setup.py test -m haufe.stripml.tests

致谢

感谢Gottfried Ganssauge将其翻译为C++编程语言。

首先,我们想要测试stripml方法。

>>> from haufe.stripml import stripml
>>> stripml.__doc__
'stripml(s) -> string'

唯一的参数是一个字符串。

>>> stripml('foo')
'foo'
>>> type(stripml('foo')) == type('')
True

stripml方法也支持unicode。

>>> stripml(u'bar')
u'bar'
>>> type(stripml(u'foo')) == type(u'')
True

尝试将整数作为第一个参数。应该引发TypeError。

>>> try:
...     stripml(10)
... except TypeError, strerror:
...     print strerror
String or unicode string required.

空脚本

>>> stripml ('<script>')
''
>>> stripml (u'<script>')
u''
>>> stripml ('<script></script>')
''
>>> stripml (u'<script></script>')
u''

尝试一些巨大的元素名称

>>> stripml ('<some-very-long-element-name-longer-than-foreseeable>')
''
>>> stripml (u'<some-very-long-element-name-longer-than-foreseeable>')
u''

现在我们尝试一些愚蠢的HTML …

>>> stripml('<b>foo</b>')
'foo'
>>> stripml('foo <i>bar</i>.')
'foo bar.'
>>> stripml('''<font size = 12><b>Really <i>big</i> string
... </b></font>''')
'Really big string\n'

… 现在是unicode。

>>> stripml(u'<b>foo</b>')
u'foo'
>>> stripml(u'foo <i>bar</i>.')
u'foo bar.'
>>> stripml(u'''<font size = 12><b>Really <i>big</i> string
... </b></font>''')
u'Really big string\n'

有时我们有< cite >script标签,其中内容没有人需要。

>>> stripml('''We have a script in here <script language="JavaScript"
... type="text/javascript">alert('Hello, World!');</script>, dude.''')
'We have a script in here , dude.'

Unicode。

>>> stripml(u'''We have a script in here <script language="JavaScript"
... type="text/javascript">alert('Hello, World!');</script>, dude.''')
u'We have a script in here , dude.'

但另一方面,< cite >scrip-标签的内容(不带尾随的‘t’)不应该被删除

>>> stripml('<scrip>KEEP THIS</scrip>')
'KEEP THIS'
>>> stripml(u'<scrip>KEEP THIS</scrip>')
u'KEEP THIS'

也不应该删除< cite >scripting-标签

>>> stripml('<scripting>KEEP THIS</scripting>')
'KEEP THIS'
>>> stripml(u'<scripting>KEEP THIS</scripting>')
u'KEEP THIS'
那么遗忘的& lt;/ script & gt;-标签呢?
>>> stripml('KEEP <script>DO NOT KEEP THIS</script></script>THIS')
'KEEP THIS'
>>> stripml(u'KEEP <script>DO NOT KEEP THIS</script></script>THIS')
u'KEEP THIS'

一个非常长的字符串。

>>> result = stripml(u'''
... <?xml version="1.0" encoding="utf-8"?>
... <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
... <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
... <head>
... <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
... <meta name="generator" content="" />
... <meta name="keywords" content="" />
... <meta name="description" content="" />
... <title>Test document</title>
... <script language="JavaScript" type="text/javascript">
... var foo=1;
... function getFoo() {
...     return foo;
... }
... </script>
... </head>
... <body onLoad="alert('Hello, World!');">
...   <h1>Test document</h1>
...   <p>This document is<br /> <i>only for testing</i>!</p>
...   <script>getFoo();</script>
... </body>
... </html>
... ''')
>>> result.strip()
u'Test document\n\n\n\n  Test document\n  This document is only for testing!'
>>> type(result)
<type 'unicode'>

单个“小于”或“大于”将被传递。

>>> stripml(u'<strong>hundred < thousand < million.</strong>')
u'hundred < thousand < million.'
>>> stripml(u'<strong>thousand > hundred.</strong>')
u'thousand > hundred.'
>>> stripml('<strong>hundred < thousand < million.</strong>')
'hundred < thousand < million.'
>>> stripml('<strong>thousand > hundred.</strong>')
'thousand > hundred.'

让我们看看一个非常长的字符串是否可以很好地处理。

>>> s = 5000 * u'<p>This is <span>a span within a paragraph.</span><!-- And this is a comment --></p>\n'
>>> stripml(s) == 5000 * u'This is a span within a paragraph.\n'
True

我们还应该看看实体和编码。

>>> stripml(u'In Stra&szlig;e und &Uuml;berf&uuml;hrung haben wir Umlaute.')
u'In Stra&szlig;e und &Uuml;berf&uuml;hrung haben wir Umlaute.'
>>> stripml('In Stra&szlig;e und &Uuml;berf&uuml;hrung haben wir Umlaute.')
'In Stra&szlig;e und &Uuml;berf&uuml;hrung haben wir Umlaute.'
>>> print stripml(u'In Straße und Überführung haben wir Umlaute.').encode('ISO-8859-1') == u'In Straße und Überführung haben wir Umlaute.'.encode('ISO-8859-1')
True

变更

1.2.2 (2012-11-07)

  • M.Honeck添加了对Visual Studio 2010的支持

1.2.1 (2008-03-20)

  • 添加了nose测试运行器支持

1.2.0 (2007-10-23)

  • 第一个公开版本。

  • 添加了许可证。

项目详情


下载文件

下载适用于您平台文件的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

haufe.stripml-1.2.2.tar.gz (12.1 kB 查看散列)

上传时间