我想在csv文件中只编写<TradingDate>
和<Product>
。
<?xml version='1.0' encoding='UTF-8'?>
<EmissionSpotMarketECarbixResults>
<Status>
<Commodity>Emission Spot ECarbix</Commodity>
<TradingDate>2018-03-14</TradingDate>
<CreationTimestamp>2018-03-14T20:08:43+01:00</CreationTimestamp>
</Status>
<Results>
<Product>
<Index>Day</Index>
<Unit>EUR/tCO2</Unit>
<IndexPrice>11,250</IndexPrice>
<Volume>3632000</Volume>
</Product>
</Results>
</EmissionSpotMarketECarbixResults>
import os
from os import walk
from xml.etree import ElementTree
import xml.etree.ElementTree as ET
for filenames in walk("D:\EEX_EMS\XML"):
(filenames)
fname= list(filenames)
for f in fname[2]:
if "EmissionSpotMarketECarbixResults" in f :
tree = ET.fromstring(f)
答案 0 :(得分:0)
使用'xmlstarlet',您可以更接近最终结果:
xmlstarlet sel -t -c //Commodity -n -c //Product -n data.xml
<Commodity>Emission Spot ECarbix</Commodity>
<Product>
<Index>Day</Index>
<Unit>EUR/tCO2</Unit>
<IndexPrice>11,250</IndexPrice>
<Volume>3632000</Volume>
</Product>
如果您知道这些字段恰好按此顺序排列,则可以使用-v而不是-c。否则你将不得不单独挑出它们。
答案 1 :(得分:0)
我试图解决它。您提到的数据存储在“data.xml”文件中,更新的xml存储在“data_update.xml”中。
看看并告诉我它是否适合您。
import xml.etree.ElementTree as ET
def main():
# Read
tree = ET.parse('data.xml')
#read and update TradingDate
trading_element = tree.find('*/TradingDate')
trading_element.text = "2011-07-09"
product_element = tree.find('*/Product')
for elem in product_element.iter():
if elem.tag == 'Index':
elem.text = "Month"
if elem.tag == 'Unit':
elem.text = "%"
if elem.tag == 'IndexPrice':
elem.text = "12,13"
if elem.tag == 'Volume':
elem.text = "4000"
#save in another file
tree.write("data_update.xml")
if __name__ == '__main__':
main()