簡體   English   中英

如何解析 XML 文件中的 VCARD

[英]How can I parse a VCARD in a XML file

我正在嘗試解析一個 XML 文件,其中有一些 VCARD。 我需要信息:FN、NOTE(SIREN 和 A)並將它們作為列表打印為 FN、SIREN_A。 如果描述中的字符串僅等於“diviseur”,我還想將它們添加到列表中

我嘗試過不同的東西(vobject,finditer),但它們都不起作用。 對於我的解析器,我使用了庫 xml.etree.ElementTree 和 pandas,它們通常會導致一些不兼容。

代碼蟒蛇:

import xml.etree.ElementTree as ET
import vobject
newlist=[]
data=[]
data.append(newlist)
diviseur=[]
tree=ET.parse('test_oc.xml')
root=tree.getroot()
newlist=[]
for lifeCycle in root.findall('{http://ltsc.ieee.org/xsd/LOM}lifeCycle'):
    for contribute in lifeCycle.findall('{http://ltsc.ieee.org/xsd/LOM}contribute'):
        for entity in  contribute.findall('{http://ltsc.ieee.org/xsd/LOM}entity'):
            vcard = vobject.readOne(entity)
            siren = vcard.contents['note'].value,":",vcard.contents['fn'].value
            print ('siren',siren.text)
    for date in contribute.findall('{http://ltsc.ieee.org/xsd/LOM}date'):
        for description in date.findall('{http://ltsc.ieee.org/xsd/LOM}description'):                       
            entite=description.find('{http://ltsc.ieee.org/xsd/LOM}string')
            print ('Type entité:', entite.text)
            newlist.append(entite)
            j=0
            for j in range(len(entite)-1):
                if entite[j]=="diviseur":
                    diviseur.append(siren[j])
                    print('diviseur:', diviseur)
                    newlist.append(diviseur)
data.append(newlist)                    
print(data)

要解析的 xml 文件:

<?xml version="1.0" encoding="UTF-8"?>    
<lom:lom xmlns:lom="http://ltsc.ieee.org/xsd/LOM" xmlns:lomfr="http://www.lom-fr.fr/xsd/LOMFR"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ltsc.ieee.org/xsd/LOM">
    <lom:version uniqueElementName="version">
        <lom:string language="http://id.loc.gov/vocabulary/iso639-2/fre">V4.1</lom:string>
    </lom:version>
    <lom:lifeCycle uniqueElementName="lifeCycle">
        <lom:contribute>
            <lom:entity><![CDATA[ 
            BEGIN:VCARD
            VERSION:4.0
            FN:Cailler
            N:;Valérie;;Mr;
            ORG:Veoli
            NOTE:SIREN=203025106
            NOTE :ISNI=0000000000000000
            END:VCARD
            ]]></lom:entity>
            <lom:date uniqueElementName="date">
                <lom:dateTime uniqueElementName="dateTime">2019-07-10</lom:dateTime>
                <lom:description uniqueElementName="description">
                    <lom:string>departure</lom:string>
                </lom:description>
            </lom:date>
        </lom:contribute>
        <lom:contribute>
            <lom:entity><![CDATA[ 
            BEGIN:VCARD
            VERSION:4.0
            FN:Besnard
            N:;Ugo;;Mr;
            ORG:MG
            NOTE:SIREN=501 025 205
            NOTE :A=0000 0000
            END:VCARD
            ]]></lom:entity>
            <lom:date uniqueElementName="date">
                <lom:dateTime uniqueElementName="dateTime">2019-07-10</lom:dateTime>
                <lom:description uniqueElementName="description">
                    <lom:string>diviseur</lom:string>
                </lom:description>
            </lom:date>
        </lom:contribute>
    </lom:lifeCycle>
</lom:lom>

回溯(最近一次調用):文件“parser_export_csv_V2.py”,第 73 行,在 vcard = vobject.readOne(entity) 文件“C:\\Users\\b\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\ site-packages\\vobject\\base.py", line 1156, in readOne allowQP)) 文件 "C:\\Users\\b\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages\\vobject\\base. py", line 1089, in readComponents for line, n in getLogicalLines(stream, allowQP): File "C:\\Users\\b\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages\\vobject\\base .py”,第 869 行,在 getLogicalLines val = fp.read(-1) AttributeError: 'xml.etree.ElementTree.Element' 對象沒有屬性 'read'

這里有幾個問題。

  • entityElement實例,vCard 是純文本數據格式。 vobject.readOne()需要文本。

  • XML 文件中的 vCard 屬性旁邊有不需要的空白。

  • NOTE :ISNI=0000000000000000無效; 它應該是NOTE:ISNI=0000000000000000 (刪除了空格)。

  • vcard.contents['note']是一個列表,沒有value屬性。

以下代碼可能無法准確生成您想要的內容,但我希望它有所幫助:

import xml.etree.ElementTree as ET
import vobject

NS = {"lom": "http://ltsc.ieee.org/xsd/LOM"}

tree = ET.parse('test_oc.xml')

for contribute in tree.findall('.//lom:contribute', NS):
    desc_string = contribute.find('.//lom:string', NS)
    print(desc_string.text)

    entity = contribute.find('lom:entity', NS)
    txt = entity.text.replace(" ", "")  # Text with spaces removed
    vcard = vobject.readOne(txt)

    for p in vcard.contents["note"]:
        print(p.name, p.value)
    for p in vcard.contents["fn"]:
        print(p.name, p.value)

    print()

輸出:

departure
NOTE SIREN=203025106
NOTE ISNI=0000000000000000
FN Cailler

diviseur
NOTE SIREN=501025205
NOTE A=00000000
FN Besnard

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM