尝试创建python脚本来显示来自复杂XML文件的数据,该文件每5-7秒更改一次。下面是我正在使用的示例XML文件...
<bsgame source="TAS Baseball/Softball" version="5.17.00"
generated="4/9/2019">
<venue gameid="SB0409-1"
visid="IUP-S" homeid="JOHN" visname="Indiana (PA)"
homename="Seton Hill"
date="4/9/2019" location="Greensburg, PA"
stadium="" duration="" attend="0"
leaguegame="N" start="3:00 PM" dhgame="1"
schedinn="7"
weather="sunny and warm">
<umpires></umpires>
<notes>
<note text="Weather: sunny and warm"></note>
<note text="Game: livegame"></note>
</notes>
<rules batters="9,9" usedh="Y"></rules>
</venue>
<team vh="V" id="IUP-S" name="Indiana (PA)" code="IUP-S">
<linescore line="0,0,0,0,1,1,3" runs="5" hits="9"
errs="2" lob="7">
<lineinn inn="1" score="0"></lineinn>
<lineinn inn="2" score="0"></lineinn>
<lineinn inn="3" score="0"></lineinn>
<lineinn inn="4" score="0"></lineinn>
<lineinn inn="5" score="1"></lineinn>
<lineinn inn="6" score="1"></lineinn>
<lineinn inn="7" score="3"></lineinn>
</linescore>
<team vh="H" id="SHU-S" name="Seton Hill" code="SHU-S">
<linescore line="0,0,0,0,0,1,X" runs="1" hits="4"
errs="2" lob="3">
<lineinn inn="1" score="0"></lineinn>
<lineinn inn="2" score="0"></lineinn>
<lineinn inn="3" score="0"></lineinn>
<lineinn inn="4" score="0"></lineinn>
<lineinn inn="5" score="0"></lineinn>
<lineinn inn="6" score="1"></lineinn>
<lineinn inn="7" score=""></lineinn>
</linescore>
我已经注意到,诸如StretchInternet之类的服务将这样的XML转换为Json以便显示数据。我已经弄清楚了如何在Python中使用xmltodict将XML转换为Json。
import json
import xmltodict
with open("/Volumes/GoogleDrive/My Drive/LIVE/XMLs/SB_Demo.xml", 'r') as f:
xmlString = f.read()
jsonString = json.dumps(xmltodict.parse(xmlString), indent=4)
print("\nJSON output(output.json):")
print(jsonString)
with open("output.json", 'w') as f:
f.write(jsonString)
我很困惑从这里去哪里。最终,我希望能够在视频广播中显示两个“团队”的全部RUNS(@runs)。但是,我是一个循序渐进的家伙。步骤1 ...如何使用xmltodict解析此XML,并仅显示两个团队的@runs?
答案 0 :(得分:0)
对于初学者,您应保持与报价用法一致(使用“或”)
此外,您的xml在每个<linescore>
之后缺少json.dumps
结束标记
关于您的问题-您正在使用with open("YOUR_XML_FILE.xml", 'r') as f:
xmlString = f.read()
xmlDict = xmltodict.parse(xmlString)
for team in xmlDict['bsgame']['team']:
print(team['@name'], 'Runs :')
print(team['linescore']['@runs'])
将xml字典转换为JSON字符串,这对于您尝试的操作将无用。将您的xmltodict对象作为Python dict访问
Indiana (PA) Runs :
5
Seton Hill Runs :
1
产生的结果
select regexp_replace('Create Or Replace Force view Schema_name.View1 as
Select /*+LEADING ACL VS TXN ACT TTY SEC SECID SHR_UDF*/
TXN_ID,
/**** IF YOU ADD COL CHECK OUT THE TEMP TABLE NAME****/
/**** IF YOU ADD COL CHECK OUT THE TEMP1 TABLE NAME****/
/**** IF YOU ADD COL CHECK OUT THE TEMP2 TABLE NAME****/
/**** IF YOU ADD COL CHECK OUT THE TEMP3 TABLE NAME****/
/**** IF YOU ADD COL CHECK OUT THE TEMP4 TABLE NAME****/
/**** IF YOU ADD COL CHECK OUT THE TEMP5 TABLE NAME****/
/**** IF YOU ADD COL CHECK OUT THE TEMP6 TABLE NAME****/
/**** IF YOU ADD COL CHECK OUT THE TEMP7 TABLE NAME****/
/**** IF YOU ADD COL CHECK OUT THE TEMP8 TABLENAME****/
/****SELECT * FROM ALL_TAB_COLUMN****/
/**** wHERE COLUMN_NAME=COL2****/
/**** gROUP BY TABLE_NAME***/
/**** oRDER BY1****/
/* CHANGE JIRA1234 STARTS*/
ACL.aCCOUNT_NUMBER,
/* CHANGE JIRA1234 ENDS*/
TXN. TXN_SALE_dATE,
TXN.TXN_iNVOICE_DATE,
TXN. TXN_pUBLISH_DATE,
ACL.TXN_EXCH_DATE,
ACL.SALES_DATE,
ACL.pRICE_DATE,
--ACL.LAST_UPDATED,
TTY.TTY_TTY_TYPE_ID,
/* CHANGE JIRA7890 STARTS*/
TXN.ACL_OPEM_ENTRY_NO,
/* CHANGE JIRA7890 eNDS*/
FROM SCHEMA.aCCOUNT_MASTER ACL
JOIN SCHEMA.TXN_MASTER TXN
ON ACL.BRANCH_CD=TXN.BRANCH_CD
AND ACL.aCCOUNT_NO=TXN.aCCOUNT_NO
JOIN SCHEMA.TXN_TYPE_MASTER TTY
ON TXN.TTY_TYPE_ID=TTY.TTY_TYPE_ID
AND TTY.SCE_ID=TXN_SEC_ID', '/\*\*.+\*\*/\s+|/\*\s.+\*/\s+|--.+\s+', '', 1, 0) from dual;