有几天我被卡住了;我想解析一个大文档,里面有很多页面,其中有像下面这样的表格:
Col1 Col2 Col3 Col4
TXT1 TTT Fnam1, LNam1
TXT2 TEE Fnam2, Mnam LNam2
TXT1
TXT5 ART Fnam3, LNam3
TXT6 BGT Fnam4, LNam4我已经用arpeggio (python)编写了语法,如果不是所有的单元格都是空的,我可以解析表格,但如果有一个或多个空单元格,所有的东西都会有一部分。有谁知道我怎样才能做到不使用模棱两可的语法?提前感谢
发布于 2021-03-27 10:57:01
多亏了@JeffC的提示,我通过询问空格的数量找到了解决方案。下面是语法:
from __future__ import print_function, unicode_literals
import os
from arpeggio import ParserPython
from arpeggio import Optional, ZeroOrMore, OneOrMore, EOF, PTNodeVisitor, ParserPython, visit_parse_tree, Combine
from arpeggio.export import PMDOTExporter, PTDOTExporter
from arpeggio import RegExMatch as _
def wsx() : return _(r"[ \t]*")
def ws2() : return _(r"[ \t]{2}")
def ws16() : return _(r"[ \t]{16}")
def code(): return _(r"[a-zA-Z0-9]{2,}")
def col4_title(): return "Col4"
def col3_title(): return "Col3"
def col2_title(): return "Col2"
def col1_title(): return "Col1"
def cell_col4(): return [Combine(wsx,OneOrMore(word,Optional(ws2)),Optional(",", OneOrMore(ws2,word))),wsx]
def cell_col3(): return [code,wsx]
def cell_col2(): return [code,wsx]
def cell_col1(): return [code,wsx]
def team_row(): return ws16, Optional(cell_col1),Optional(ws2),Optional(cell_col2),Optional(ws2),Optional(cell_col3),Optional(ws2),Optional(cell_col4),Optional(wsx), nl
def team_table_body(): return wsx, OneOrMore(nl,team_row),
def team_table_title(): return "testtab"
def team_table_header(): return wsx, team_table_title, wsx, col1_title, wsx, col2_title, wsx, col3_title, wsx, col4_title,Optional(wsx),nl
def team_block(): return wsx, team_table_header, team_table_bodyhttps://stackoverflow.com/questions/66810272
复制相似问题