Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
165 views
in Technique[技术] by (71.8m points)

python - Extracting Text from "Pseudo" HTML

I am trying to rebuild work orders from a Manufacturing Execution System (MES) SQL database into .pdf form so that they can be printed en masse--as opposed to one at a time (one at a time is the only means the MES allows for).

I am stuck when it comes to the work instructions that contain links and etc (the pseudo-html...not sure what else to call it). I run the SQL query for the data needed and put it into a Pandas dataframe. The following is an example of the "Text" column (the work instructions) in the dataframe:

"DWG/TECH DATA: ALL TASK WITHIN THIS WORK ORDER ARE TO BE ACCOMPLISHED IAW:

<#Tab><UT=""LinkInvoke(Slide(OBJECT_ID=OBJECTID,@GlyphName=@InlineText,@Classification=General,@RenderDescText=True,'@Desc=| Description: PANEL, |',@Caption=DWG 123456-123 ,REF_ID=REFID))""><#Tab>

MOA DWG:
<#Tab><UT=""LinkInvoke(Slide(OBJECT_ID=OBJECT ID,@GlyphName=@InlineText,@Classification=General,@RenderDescText=True,'@Desc=| Description: FACEPLATES |',@Caption=DWG 98765 Plate,REF_ID=REFID))"">
<#Tab><UT=""LinkInvoke(Slide(OBJECT_ID=OBJID,@GlyphName=@InlineText,@Classification=General,@RenderDescText=True,'@Desc=| Description: ARTWORK |',@Caption=DWG 9999-8888 ARTWORK ,REF_ID=REFID))""><#Tab>"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With string manipulation (not regex), something along these lines works:

work = '''DWG/TECH DATA: ALL TASK WITHIN THIS WORK ORDER ARE TO BE ACCOMPLISHED IAW:
<#Tab><UT=""LinkInvoke(Slide(OBJECT_ID=OBJECTID,@GlyphName=@InlineText,@Classification=General,@RenderDescText=True,'@Desc=| Description: PANEL, |',@Caption=DWG 123456-123 ,REF_ID=REFID))""><#Tab>
MOA DWG:
<#Tab><UT=""LinkInvoke(Slide(OBJECT_ID=OBJECT ID,@GlyphName=@InlineText,@Classification=General,@RenderDescText=True,'@Desc=| Description: FACEPLATES |',@Caption=DWG 98765 Plate,REF_ID=REFID))"">
<#Tab><UT=""LinkInvoke(Slide(OBJECT_ID=OBJID,@GlyphName=@InlineText,@Classification=General,@RenderDescText=True,'@Desc=| Description: ARTWORK |',@Caption=DWG 9999-8888 ARTWORK ,REF_ID=REFID))""><#Tab>"
'''

work_dat = work.splitlines()
for line in work_dat:
    line_lst = line.split('|')
    step_1 = [item  if "@Caption=" in item else line_lst for item in line_lst][0]
    step_2 = [item if len(step_1)==1 else step_1[2] for item in step_1]
    if len(step_2)>1:
        print(step_2[1].split('=')[1].split(',')[0].strip())
    else:
        print(step_2[0])

Output:

DWG/TECH DATA: ALL TASK WITHIN THIS WORK ORDER ARE TO BE ACCOMPLISHED IAW:
DWG 123456-123
MOA DWG:
DWG 98765 Plate
DWG 9999-8888 ARTWORK

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...