我从一个在线数据库下载了文件。所有的文件都包含一两段CAML格式的段落,这对我来说真的很不方便。
例如:
"<caml:Content xmlns:caml="http://lc.ca.gov/legalservices/schemas/caml.1#"><p>(a)<span class="EnSpace"/>Every home solicitation contract or offer for home improvement goods or services which provides for a lien on real property is subject to the provisions of Chapter 1 (commencing with Section 1801) of Title 2 of Part 4 of Division 3.</p><p>(b)<span class="EnSpace"/>For purposes of this section, “home improvement goods or services” means goods and services, as defined in Section 1689.5, which are bought in connection with the improvement of real property. Such home improvement goods and services include, but are not limited to, burglar alarms, carpeting, texture coating, fencing, air conditioning or heatingnequipment, and termite extermination. Home improvement goods include goods which, at the time of sale or subsequently, are to be so affixed to real property as to become a part of real property whether or not severable therefrom.</p></caml:Content>"
(这存储在一个.lob文件中——我有成千上万个这样的文件(
我有一个shell脚本来将这些数据(存储在数千个文件中(推送到我的本地数据库中。我打算在我的应用程序中使用这些数据,但我只需要文本部分,而不需要所有的CAML标记。
我正在寻找一个脚本或工具,将所有CAML段落转换为所有文件中的纯文本。然后我将重新填充我的MySQL本地数据库。有人知道最好的方法是什么吗?
谢谢!!
经过一段时间的尝试,我使用的方法是使用python脚本。更具体地说,我使用了beautifulsoup库。
像这样:
import bs4 as bs
source = """"<caml:Content xmlns:caml="http://lc.ca.gov/legalservices/schemas/caml.1#"><p>(a)<span class="EnSpace"/>Every home solicitation contract or offer for home improvement goods or services which provides for a lien on real property is subject to the provisions of Chapter 1 (commencing with Section 1801) of Title 2 of Part 4 of Division 3.</p><p>(b)<span class="EnSpace"/>For purposes of this section, “home improvement goods or services” means goods and services, as defined in Section 1689.5, which are bought in connection with the improvement of real property. Such home improvement goods and services include, but are not limited to, burglar alarms, carpeting, texture coating, fencing, air conditioning or heatingnequipment, and termite extermination. Home improvement goods include goods which, at the time of sale or subsequently, are to be so affixed to real property as to become a part of real property whether or not severable therefrom.</p></caml:Content>"""
soup = bs.BeautifulSoup(source, "lxml")
print(soup.get_text())
输出:
"(a)Every home solicitation contract or offer for home improvement goods or services which provides for a lien on real property is subject to the provisions of Chapter 1 (commencing with Section 1801) of Title 2 of Part 4 of Division 3.(b)For purposes of this section, “home improvement goods or services” means goods and services, as defined in Section 1689.5, which are bought in connection with the improvement of real property. Such home improvement goods and services include, but are not limited to, burglar alarms, carpeting, texture coating, fencing, air conditioning or heating
equipment, and termite extermination. Home improvement goods include goods which, at the time of sale or subsequently, are to be so affixed to real property as to become a part of real property whether or not severable therefrom.