2 回答

TA贡献1873条经验 获得超9个赞
有两种方法。两者都是超级笨拙的,并且非常依赖于原始字符串的非常小的波动。但是,您可以修改代码以提供更多的灵活性。
这两个选项都取决于满足这些特征的线......有问题的分组必须......
以字母或斜线开头,可能大写
该感兴趣的标题后跟一个冒号(“:”)
仅抓住冒号后的第一个单词。
方法一,正则表达式,这个只能抓取两块数据。第二组是“其他所有内容”,因为我无法正确重复搜索模式:P
代码:
import re
l = [ 'MC/MX/FF Number(s): None DUNS Number: -- ', 'Power Units: 1 Drivers: 1 ' ]
pattern = ''.join([
"(", # Start capturing group
"\s*[A-Z/]", # Any number of space, until and including only the first capital or forward slash
".+?\:", # any character (non-greedy) up to and including the colon
"\s*", # One or more spaces
"\w+\s*", # One or more alphanumeric chars i.e. [a-zA-Z0-9]
")", # End capturing group
"(.*)"
])
for s in l:
m = re.search(pattern, s)
print("----------------")
try:
print(m.group(1))
print(m.group(2))
print(m.group(3))
except Exception as e:
pass
输出:
----------------
MC/MX/FF Number(s): None
DUNS Number: --
----------------
Power Units: 1
Drivers: 1
方法二,逐字解析字符串。此方法具有与正则表达式相同的基本特征,但可以执行两个以上感兴趣的块。它的工作原理...
开始逐字解析每个字符串,并将其加载到
newstring
.当它碰到冒号时,标记一个标志。
将下一个循环中的第一个单词添加到
newstring
. 如果需要,您可以将其更改为 1-2、1-3 或 1-n 字。您也可以让它在colonflag
设置后继续添加单词,直到满足某些条件,例如带有大写字母的单词……尽管这可能会中断诸如“无”之类的单词。你可以一直到遇到一个全大写的单词,但是一个非全大写的标题会破坏它。添加
newstring
到newlist
,重置标志,并继续解析单词。
代码:
s = 'MC/MX/FF Number(s): None DUNS Number: -- '
for s in l:
newlist = []
newstring = ""
colonflag = False
for w in s.split():
newstring += " " + w
if colonflag:
newlist.append(newstring)
newstring = ""
colonflag = False
if ":" in w:
colonflag = True
print(newlist)
输出:
[' MC/MX/FF Number(s): None', ' DUNS Number: --']
[' Power Units: 1', ' Drivers: 1']
第三个选项: 创建所有预期标头的列表,例如header_list = ["Operating Status:", "Out of Service Date:", "MC/MX/FF Number(s):", "DUNS Number:", "Power Units:", "Drivers:", ]
并根据这些标头进行拆分/解析。
第四种选择
使用自然语言处理和机器学习来实际找出逻辑句子的位置;)

TA贡献1830条经验 获得超3个赞
看看pyparsing。这似乎是表达单词组合、检测它们之间的关系(以语法方式)并产生结构化响应的最“自然”的方式......网上有很多教程和文档:
您可以使用 `pip install pyparsing' 安装 pyparsing
解析:
Operating Status: NOT AUTHORIZED Out of Service Date: None
需要类似的东西:
!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# test_pyparsing2.py
#
# Copyright 2019 John Coppens <john@jcoppens.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
import pyparsing as pp
def create_parser():
opstatus = pp.Keyword("Operating Status:")
auth = pp.Combine(pp.Optional(pp.Keyword("NOT"))) + pp.Keyword("AUTHORIZED")
status = pp.Keyword("Out of Service Date:")
date = pp.Keyword("None")
part1 = pp.Group(opstatus + auth)
part2 = pp.Group(status + date)
return part1 + part2
def main(args):
parser = create_parser()
msg = "Operating Status: NOT AUTHORIZED Out of Service Date: None"
print(parser.parseString(msg))
msg = "Operating Status: AUTHORIZED Out of Service Date: None"
print(parser.parseString(msg))
return 0
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))
运行程序:
[['Operating Status:', 'NOT', 'AUTHORIZED'], ['Out of Service Date:', 'None']]
[['Operating Status:', '', 'AUTHORIZED'], ['Out of Service Date:', 'None']]
使用Combine,Group您可以更改输出的组织方式。
添加回答
举报