Python實現(xiàn)將Excel轉(zhuǎn)換成xml的方法示例
本文實例講述了Python實現(xiàn)將Excel轉(zhuǎn)換成xml的方法。分享給大家供大家參考,具體如下:
最近寫了個小工具 用于excel轉(zhuǎn)成xml
直接貼代碼吧:
#coding=utf-8 import xlrd import datetime import time import sys import xml.dom.minidom import os print sys.getdefaultencoding() reload(sys) #就是這么坑爹,否則下面會報錯 sys.setdefaultencoding('utf-8') #py默認是ascii。。要設(shè)成utf8 #excel中 數(shù)據(jù)格式如下: # UID 第四天 # 1579880025 10:00-13:30 # 1677982825 10:00-12:00 # 1704410718 10:00-12:00 # 83713892 10:00-12:00 # 1546551561 10:00-12:00 # 1298790776 10:00-12:00 def open_excel(file): try: data = xlrd.open_workbook(file) #xlrd 操作excel的外部庫 return data except Exception, e: print str(e) bgntm = '2017-05-18_' def get_time_t(stime): stime = bgntm + stime + ':00' # return time.strptime(stime, '%Y-%m-%d %H:%M:%S') #將時間轉(zhuǎn)成時間戳 return stime def excel_table_byindex(file, colnnameindex=0, by_index=0): data = open_excel(file) #打開excel table = data.sheets()[by_index] nrows = table.nrows ncols = table.ncols doc = xml.dom.minidom.Document() #打開xml對象 xmain = doc.createElement('main') doc.appendChild(xmain) for nrow in range(0, nrows): #遍歷每一行 if nrow == 0: continue uid = table.cell(nrow, 0).value #取值..第一列 item = doc.createElement('%d'%uid) #生成節(jié)點 stime = table.cell(nrow, 1).value #第二列的值 stime = stime.strip() #去除空格..excel數(shù)據(jù)里 經(jīng)常會無意有蛋疼的多余空格 listT = stime.split('-') #按 -分割字符串 # sbgn = 'bgn = %d'%time.mktime(get_time_t(listT[0])) sbgn = 'bgn = '+get_time_t(listT[0]) print 'uid=%d'%uid print 'bgn:'+sbgn send = 'end = '+get_time_t(listT[1]) # send = 'end = %d'%time.mktime(get_time_t(listT[1])) print 'end:'+send exxbgn = doc.createTextNode(sbgn) #純文本節(jié)點 exxend = doc.createTextNode(send) item.appendChild(exxbgn) #加入樹中 item.appendChild(exxend) # ebgn = doc.createElement('bgn') # eend = doc.createElement('bgn') # item.appendChild(ebgn) # item.appendChild(eend) # item.setAttribute('bgn', '%d'%time.mktime(get_time_t(listT[0]))) #設(shè)置節(jié)點屬性 # item.setAttribute('end', '%d'%time.mktime(get_time_t(listT[1]))) # for lt in listT: # print time.mktime(get_time_t(lt)) xmain.appendChild(item) f = open('G:/testPro/py/exceltoxml/day.xml', 'w') #xml文件輸出路徑 f.write(doc.toprettyxml()) f.close() excel_table_byindex('G:/testPro/py/exceltoxml/day.xlsx') #excel文件路徑
關(guān)于xlrd 可以在cmd里pip install xlrd
來安裝
PS:這里再為大家提供幾款關(guān)于xml操作的在線工具供大家參考使用:
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson
在線格式化XML/在線壓縮XML:
http://tools.jb51.net/code/xmlformat
XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress
XML代碼在線格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python操作xml數(shù)據(jù)技巧總結(jié)》、《Python操作Excel表格技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
python 通過字符串調(diào)用對象屬性或方法的實例講解
下面小編就為大家分享一篇python 通過字符串調(diào)用對象屬性或方法的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04python實現(xiàn)RabbitMQ的消息隊列的示例代碼
這篇文章主要介紹了python實現(xiàn)RabbitMQ的消息隊列的示例代碼,總結(jié)了RabbitMQ中三種exchange模式的實現(xiàn),分別是fanout, direct和topic。感興趣的小伙伴們可以參考一下2018-11-11Python英文詞頻統(tǒng)計(哈姆雷特)程序示例代碼
在文本處理方面,Python也有著得天獨厚的優(yōu)勢,不僅提供了多種字符串操作函數(shù),而且還可以使用各種開源庫來處理文本,下面這篇文章主要給大家介紹了關(guān)于Python英文詞頻統(tǒng)計(哈姆雷特)程序示例的相關(guān)資料,需要的朋友可以參考下2023-06-06PyCharm實現(xiàn)遠程調(diào)試的全過程(附圖文講解)
這篇文章主要介紹了PyCharm實現(xiàn)遠程調(diào)試的全過程,文中通過圖文結(jié)合的方式給大家介紹的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下2024-05-05解決安裝python3.7.4報錯Can''''t connect to HTTPS URL because the S
這篇文章主要介紹了解決安裝python3.7.4報錯Can't connect to HTTPS URL because the SSL module is not available,本文給大家簡單分析了錯誤原因,給出了解決方法,需要的朋友可以參考下2019-07-07