1. 程式人生 > >python -初級學習—xml

python -初級學習—xml

  以前工作中從來沒有用過python,這是第一次接觸,硬著頭皮寫完了xml檔案的編寫過程,感覺python還挺好用的,尤其是在虛機上。以下是python實現xml的過程,挺簡單的,但我擔心以後不用了,下一次用還是找不到,就寫到部落格中了吧。

rom __future__ import division
from __future__ import print_function

from datetime import datetime
from xml.dom import minidom
import math
import time
import numpy as np
import sys
import csv
import os
import codecs
import cv2
import stat


def GenerateXml():
	import xml.dom.minidom
	print('start')
	impl = xml.dom.minidom.getDOMImplementation()
	dom = impl.createDocument(None, 'CropImageInformation', None)
	root = dom.documentElement
	name = dom.createElement('name')
	nameT= dom.createTextNode('010401')
	name.appendChild(nameT)
	root.appendChild(name)

	inbound_folder = '/mnt/share'
    	for f in os.listdir(inbound_folder):
      	   fPath = os.path.join(inbound_folder, f)
	   #print(f)
     	   if os.path.isfile(fPath) and os.path.splitext(fPath)[1].lower() == ".jpg":
	      img = cv2.imread(fPath)
	      nWidth = img.shape[1]
	      nHeight = img.shape[0]
	      nLength = len(fPath)
	      nLength1 = nLength-7
	      nLength2 = nLength-21
	      startDate = fPath[nLength2:nLength1]
	      namepicture = f
	      namexml = f[0:len(f)-4]
	      
	      

        sWidth = str(nWidth)
        Resolution = dom.createElement('imageResolution')
	pixelROw = dom.createElement('pixelROw')
	numRow=dom.createTextNode(sWidth)
	pixelROw.appendChild(numRow)
	Resolution.appendChild(pixelROw)

	sHeight = str(nHeight)
	pixelCol = dom.createElement('pixelCol')
	numCol=dom.createTextNode(sHeight)
	pixelCol.appendChild(numCol)
	Resolution.appendChild(pixelCol)
	root.appendChild(Resolution)


	stage='0'
	with open('/home/qinxue/golucky/outbound/XIAOBAI.csv','rb') as csvfile:
	    reader = csv.reader(csvfile)
	    #print(namepicture)
	    for row in reader:
		if row[2] == namepicture:
	    	  stage = row[4]
		  #print(stage)
		  break


	stageflag = dom.createElement('stageflag')
	numStageflag = dom.createTextNode(stage )
	stageflag.appendChild(numStageflag)
	root.appendChild(stageflag)
	
	stageStartDate = dom.createElement('stageStartDate')
	Date = dom.createTextNode(startDate)
	stageStartDate.appendChild(Date)
	root.appendChild(stageStartDate)

	days = dom.createElement('days')
	DayNum = dom.createTextNode('1')
	days.appendChild(DayNum)
	root.appendChild(days)

	percentOfStage = dom.createElement('percentOfStage')
	root.appendChild(percentOfStage)

	currentCoverage = dom.createElement('currentCoverage')
	Coverage = dom.createTextNode('12')
	currentCoverage.appendChild(Coverage)
	root.appendChild(currentCoverage)
	
	yesterdayMeanCoverage = dom.createElement('yesterdayMeanCoverage')
	yesterdayCoverage = dom.createTextNode('3')
	yesterdayMeanCoverage.appendChild(yesterdayCoverage)
	root.appendChild(yesterdayMeanCoverage)

	leafAreaIndex = dom.createElement('leafAreaIndex')
	leafArea = dom.createTextNode('6')
	leafAreaIndex.appendChild(leafArea)
	root.appendChild(leafAreaIndex)	

	height = dom.createElement('height')
	root.appendChild(height)

	numplant = '0'
	nLengthString = len("/home/qinxue/golucky/test/")
	with open('/home/qinxue/golucky/outbound/LUCKY.csv','rb') as csvfile:
	    reader = csv.reader(csvfile)
	    for row in reader:
	       luckyname = row[0]
	       nLengthlucky = len(luckyname)
	       if luckyname[nLengthString:nLengthlucky] == namepicture:
		  numplant = (float)(row[1])
		  break


	parapath = "/mnt/share/Area.ini"  
   	filopen = open(parapath,"r")
        line = filopen.readline()
	fArea = float(line[0])
	fDensity = numplant / fArea

	density = dom.createElement('density')
	num = dom.createTextNode(str(fDensity))
	density.appendChild(num)
	root.appendChild(density)
	

	amountOfGrowth = dom.createElement('amountOfGrowth')
	root.appendChild(amountOfGrowth)	

	growthState = dom.createElement('growthState')
	root.appendChild(growthState)

	observationArea = dom.createElement('observationArea')
	Area = dom.createTextNode('2551')
	observationArea.appendChild(Area)
	root.appendChild(observationArea)
	
	warningInfor = dom.createElement('warningInfor')
	Infor = dom.createTextNode('1100000000')
	warningInfor.appendChild(Infor)
	root.appendChild(warningInfor)

	filename = '/mnt/share/'+namexml +'.xml'
	print(filename)
	#os.mknod(filename)
	f= open(filename, 'w')
	dom.writexml(f, addindent=' ', newl='\n')
	f.close()

if __name__ == '__main__':
    GenerateXml()