簡體   English   中英

解析數據集的字符串

[英]Parsing a string for data sets

我有一個長字符串,其中包含需要分開/提取以進行評估的信息集。

作為一個初學者程序員,我知道各種解析操作,例如split()append()remove()等-但我正在努力提出一種將它們組合以提取相關數據的邏輯方法...

長串...

"<Sets X="s"><B s="1" e="2176" t="-2.0774E4" r="1" /><B s="2177" e="8982" t="-1.8597E4" r="1" /><B s="8983" e="10393" t="-150.22" r="1" /></Sets>"

包含3組數據需要存儲為浮點值

[Set1] s = 1 e = 2176 t = -20774 r = 1

[Set2] s = 2177 e = 8982 t = -18597 r = 1

[Set3] s = 8983 e = 10393 t = -150.2 r = 1

我希望將每組數據存儲為一個列表

Set1 = [1,2176,-20774,1]
Set2 = [2177,8982,-18597,1]
Set3 = [2178,10393,-150.2,1]

注意:套數可以變化

使用內置的ElementTree庫從xml中提取數據:

import xml.etree.ElementTree as ET


data = '<Sets X="s"><B s="1" e="2176" t="-2.0774E4" r="1" /><B s="2177" e="8982" t="-1.8597E4" r="1" /><B s="8983" e="10393" t="-150.22" r="1" /></Sets>'

tree = ET.fromstring(data)
for b in tree.findall('.//B'):
     print map(float, itemgetter(*'setr')(b.attrib))

印刷品:

[1.0, 2176.0, -20774.0, 1.0]
[2177.0, 8982.0, -18597.0, 1.0]
[8983.0, 10393.0, -150.22, 1.0]

注意:這是先前答案的擴展...

(@alecxe和@Jon Clements的道具)

為了標記每個數據集並以易於訪問的格式存儲結果

import xml.etree.ElementTree as ET
import operator

data = '<Sets X="s"><B s="1" e="2176" t="-2.0774E4" r="1" /><B s="2177" e="8982" t="-1.8597E4" r="1" /><B s="8983" e="10393" t="-150.22" r="1" /></Sets>'

dataDictionary = {}

tree = ET.fromstring(data)
setNumber = 0

for b in tree.findall('.//B'):
    setNumber = setNumber + 1
    dataSet = map(float, operator.itemgetter(*'setr')(b.attrib))
    dataDictionary[setNumber] = dataSet
    print "This is dataset " +str(setNumber)
    print dataSet

print ""
print "This is the Dictionary of datasets"
print dataDictionary

這將導致以下輸出-易於將來操作使用:)

This is dataSet 1
[1.0, 2176.0, -20774.0, 1.0]
This is dataSet 2
[2177.0, 8982.0, -18597.0, 1.0]
This is dataSet 3
[8983.0, 10393.0, -150.22, 1.0]

This is the dataDictionary
{1: [1.0, 2176.0, -20774.0, 1.0], 2: [2177.0, 8982.0, -18597.0, 1.0], 3: [8983.0, 10393.0, -150.22, 1.0]}

暫無
暫無

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

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