亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從我的 XML 文件中提取信息并為其分配一個向量

從我的 XML 文件中提取信息并為其分配一個向量

蠱毒傳說 2023-12-05 15:01:47
我想用 python 解析我的計算機上的一些 XML 文件并從每個文件中提取一些信息這是我的其中之一的 xml 文件:(如果您想要文本在這里: https://github.com/peldszus/arg-microtexts/blob/master/corpus/en/micro_b002.xml)作為第一級,我已經完成了第一級:myList = []                #read the whole text from for root, dirs, files in os.walk(path):    for file in files:        if file.endswith('.xml'):            with open(os.path.join(root, file), encoding="UTF-8") as content:                tree = ET.parse(content)                myList.append(tree)在 myList 中,我有一些 XMl 文件 <xml.etree.ElementTree.ElementTree at 0x1f0fb1f8430>現在對于根“邊緣”,它們沒有 type="seg" <edge id="c1" src="a1" trg="a3" type="sup"/>  <edge id="c2" src="a2" trg="a3" type="sup"/>  <edge id="c4" src="a4" trg="a3" type="reb"/>  <edge id="c5" src="a5" trg="c4" type="und"/>我想提取標簽“src”,我想提取標簽=Src,  src="a1"    src="a2"    src="a4"   src="a5" 然后我想分配的數字不在src中,因為這句話稱為前提,例如這里...我想說“a3”是所謂的“前提”(因為它不是標簽src)例如這里(0,0,1,0,0) 應該是我的過程的結果,因為 a3 沒有被應用,我將第三個數組設置為 1,其余的設置為零一般來說,我想提取信息以注釋我的文本,該文本已使用 xml 進行了一些注釋
查看完整描述

3 回答

?
德瑪西亞99

TA貢獻1770條經驗 獲得超3個贊

您的問題中并非所有內容都清楚...

以下是數據提取部分


import xml.etree.ElementTree as ET


xml = '''<?xml version='1.0' encoding='UTF-8'?>

<arggraph id="micro_b002" topic_id="higher_dog_poo_fines" stance="pro">

  <edu id="e1"><![CDATA[One can hardly move in Friedrichshain or Neuk?lln these days without permanently scanning the ground for dog dirt.]]></edu>

  <edu id="e2"><![CDATA[And when bad luck does strike and you step into one of the many 'land mines' you have to painstakingly scrape the remains off your soles.]]></edu>

  <edu id="e3"><![CDATA[Higher fines are therefore the right measure against negligent, lazy or simply thoughtless dog owners.]]></edu>

  <edu id="e4"><![CDATA[Of course, first they'd actually need to be caught in the act by public order officers,]]></edu>

  <edu id="e5"><![CDATA[but once they have to dig into their pockets, their laziness will sure vanish!]]></edu>

  <adu id="a1" type="pro"/>

  <adu id="a2" type="pro"/>

  <adu id="a3" type="pro"/>

  <adu id="a4" type="opp"/>

  <adu id="a5" type="pro"/>

  <edge id="c6" src="e1" trg="a1" type="seg"/>

  <edge id="c7" src="e2" trg="a2" type="seg"/>

  <edge id="c8" src="e3" trg="a3" type="seg"/>

  <edge id="c9" src="e4" trg="a4" type="seg"/>

  <edge id="c10" src="e5" trg="a5" type="seg"/>

  <edge id="c1" src="a1" trg="a3" type="sup"/>

  <edge id="c2" src="a2" trg="a3" type="sup"/>

  <edge id="c4" src="a4" trg="a3" type="reb"/>

  <edge id="c5" src="a5" trg="c4" type="und"/>

</arggraph>'''

root = ET.fromstring(xml)

interesting_edges_src = [e.attrib['src'] for e in root.findall('.//edge') if e.attrib['type'] != 'seg' ]

print(interesting_edges_src)

輸出


['a1', 'a2', 'a4', 'a5']


查看完整回答
反對 回復 2023-12-05
?
手掌心

TA貢獻1942條經驗 獲得超3個贊

這里可以被認為是某種接近最終答案的答案


myList = []??

myEdgesList=[]

#read the whole text from?

for root, dirs, files in os.walk(path):

? ? for file in files:

? ? ? ? if file.endswith('.xml'):

? ? ? ? ? ? with open(os.path.join(root, file), encoding="UTF-8") as content:

? ? ? ? ? ? ? ? tree = ET.parse(content)

? ? ? ? ? ? ? ? myList.append(tree)

? ? ? ? ? ? ? ??

for k in myList:

? ? Edge= [e.attrib['src'] for e in k.findall('.//edge') if e.attrib['type'] != 'seg' ]

? ? myEdgesList.append(Edge)

這提供


['a1', 'a2', 'a4', 'a5'] 對于上面的示例以及所有其他示例的列表


[['a1', 'a2', 'a3', 'a4'],

?['a1', 'a2', 'a4', 'a5'],

?['a1', 'a2', 'a4', 'a5'],

?['a2', 'a3', 'a4', 'a5'],

?['a2', 'a3', 'a4', 'a5'],

?['a2', 'a3', 'a4', 'a5'],

?['a1', 'a2', 'a4', 'a5'],

?['a1', 'a2', 'a4', 'a5'],

?['a1', 'a2', 'a4', 'a5'],

?['a1', 'a2', 'a4', 'a5'],

?['a2', 'a3', 'a4', 'a5'],

?['a2', 'a3', 'a4', 'a5'],

?['a2', 'a3', 'a4', 'a5'],

?['a2', 'a3', 'a4', 'a5'],

?['a2', 'a3', 'a4', 'a5'],

?['a2', 'a3', 'a4', 'a5'],

?['a2', 'a3', 'a4', 'a5'],

?['a2', 'a3', 'a4', 'a5'],

?['a2', 'a3', 'a4', 'a5'],

?['a2', 'a3', 'a4', 'a5'],

?['a2', 'a3', 'a4', 'a5'],

?['a1', 'a2', 'a4', 'a5'],

?['a1', 'a2', 'a4', 'a5'],

?['a1', 'a2', 'a4', 'a5'],

?['a1', 'a2', 'a4', 'a5'],

?['a1', 'a2', 'a4', 'a5'],

?['a1', 'a2', 'a4', 'a5'],

?['a1', 'a2', 'a4', 'a5'],

?['a1', 'a2', 'a3', 'a4'],

?['a1', 'a2', 'a3', 'a4'],

?['a1', 'a2', 'a3', 'a4'],

?['a1', 'a2', 'a3', 'a4'],

?['a1', 'a2', 'a3', 'a4'],

?['a1', 'a2', 'a3', 'a4'],

?['a1', 'a2', 'a3', 'a4'],

?['a1', 'a2', 'a3', 'a4'],

?['a2', 'a3', 'a4'],

?['a2', 'a3', 'a4'],

?['a2', 'a3', 'a4'],

?['a2', 'a3', 'a4'],

?['a2', 'a3', 'a4'],

?['a2', 'a3', 'a4'],

?['a2', 'a3', 'a4'],

?['a2', 'a3', 'a4'],

?['a2', 'a3', 'a4'],

?['a2', 'a3', 'a4', 'a5'],

?['a2', 'a3', 'a4', 'a5'],

?['a2', 'a3', 'a4', 'a5'],

?['a2', 'a3', 'a4', 'a5'],

?['a2', 'a3', 'a4', 'a5'],

?['a2', 'a3', 'a4', 'a5'],

?['a2', 'a3', 'a4', 'a5'],

?['a2', 'a3', 'a4', 'a5'],

?['a2', 'a3', 'a4', 'a5'],

?['a2', 'a3', 'a4', 'a5'],

?['a2', 'a3'],

?['a2', 'a3'],

?['a2', 'a3'],

?['a2', 'a3'],

?['a2', 'a3'],

?['a2', 'a3'],

?['a2', 'a3'],

?['a2', 'a3'],

?['a2', 'a3'],

?['a2', 'a3'],

?['a2', 'a3'],

?['a1', 'a2', 'a3'],

?['a1', 'a2', 'a3'],

?['a1', 'a2', 'a3'],

?['a1', 'a2', 'a3'],

?['a1', 'a2', 'a3'],

?['a1', 'a2', 'a3'],

?['a1', 'a2', 'a3'],

?['a1', 'a2', 'a3'],

?['a1', 'a2', 'a3'],

?['a1', 'a2', 'a3'],

?['a1', 'a2', 'a3'],

?['a1', 'a2', 'a3'],

?['a2', 'a3', 'a4', 'a5'],

.

.

.


只剩下將此列表轉換為



(0,0,0,0,1) <----- ['a1', 'a2', 'a3', 'a4']


#as a5 is missing?



(0,0,1,0,0) <------? ['a1', 'a2', 'a4', 'a5']


#as a3 is misisng?

.

.

.

(0,0,1)? ? <-------? ?['a2', 'a3']


#as a1 is missing?

等等


如果您有任何想法請告訴我,我也在努力


查看完整回答
反對 回復 2023-12-05
?
牧羊人nacy

TA貢獻1862條經驗 獲得超7個贊

對于下一個問題


myEdgtlistmap=[]

for lst in myEdgesList:

    tp=[]

    for el in lst:

        if el=="a1":

            tp.append(1)

        if el=="a2":

            tp.append(2)

        if el=="a3":

            tp.append(3)

        if el=="a4":

            tp.append(4)

        if el=="a5":

            tp.append(5)

        if el=="a6":

            tp.append(6)

    myEdgtlistmap.append(tp)

label=[]

for le in myEdgtlistmap:

    b=[1]*(len(le)+1)

    for v in le: 

        b[v-1]=0

    label.append(b)

y=[l for lab in label for l in lab ]


查看完整回答
反對 回復 2023-12-05
  • 3 回答
  • 0 關注
  • 195 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號