程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python uses elementtree to build XML files

編輯:Python

1、 How to build xml file ?

         Actual case :

                 Some of the time , We need to convert other formats of data into xml, for example , We need to put Ping An stock csv file , Convert to the corresponding xml.

pingan.csv:
Data,Open,High,Low,CLose,Volume,Adj Close
2016-06-30,8.69,8.74,8.66,8.70,36220400,8.70
pingan.xml:
<Data>
<Row>
<Date>2016-06-30</Date>
<Open>8.69</Open>
<High>8.74</High>
<Low>8.66</Low>
<Close>8.70</Close>
<Volume>36220400</Volume>
<AdjClose>8.70</AdjClose>
</Row>
</Data>

         Solution :

                 Use... From the standard library xml.etree.ElementTree, structure ElementTree, Use write Method write file .

2、 Code demonstration

    (1) Build element Element And the element tree ElementTree Use

from xml.etree.ElementTree import Element, ElementTree
# Create elements , Pass in tag
e = Element('Data')
print(e.tag)
# Set the properties of the element and text,get Method can get properties ,set Method can set properties
e.set('name', 'abc')
print(e.get('name'))
e.text = '123'
print(e.text)
# For the convenience of observation , Import tostring Method ,
# It can see an element become xml What kind of string to convert later
from xml.etree.ElementTree import tostring
print(tostring(e))
# The relationship between elements , You can add child elements to an element
e2 = Element('Row')
e3 = Element('Open')
e3.text = '8.69'
# Give Way open As row Child elements
e2.append(e3)
print(tostring(e2))
# And then let Row As Data Child elements
e.append(e2)
# Get rid of Data Of text
e.text = None
print(tostring(e))
# Write the element string to the file , establish ElementTree
et = ElementTree(e)
# Use write Method to write the file name directly
et.write('build_xml.xml')

    (2) Realization csv convert to xml

import csv
from xml.etree.ElementTree import Element, ElementTree
def pretty(e, level=0):
# For formatting xml data
if len(e) > 0:
e.text = '\n' + '\t' * (level + 1)
for child in e:
pretty(child, level + 1)
child.tail = child.tail[:-1]
e.tail = '\n' + '\t' * level
def csv_to_xml(f_name): # f_name For the file name
# Read csv file
with open(f_name, 'r', encoding='gb18030') as f:
reader = csv.reader(f)
# obtain csv Middle header information
headers = next(reader)
print(headers)
# common xml Root element
root = Element('Data')
for row in reader:
e_row = Element('Row')
# Add child elements to the root node
root.append(e_row)
# Iterate over each data and the corresponding tag at the same time
for tag, text in zip(headers, row):
e = Element(tag)
e.text = text
e_row.append(e)
pretty(root)
# Final return to element tree
return ElementTree(root)
et = csv_to_xml('000001.csv')
# take xml Write data to file
et.write('000001.xml')


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved