mirror of
https://github.com/panda3d/panda3d.git
synced 2025-10-17 20:23:47 -04:00
excel xml data file reader
This commit is contained in:
parent
8ba6dd5b9f
commit
a5cb0b9e0b
58
direct/src/showbase/ExcelHandler.py
Executable file
58
direct/src/showbase/ExcelHandler.py
Executable file
@ -0,0 +1,58 @@
|
||||
"""
|
||||
A simple XML parser for Excel XML data. Built on top of xml.sax
|
||||
|
||||
Example use:
|
||||
e=ExcelHandler()
|
||||
parse('myData.xml', e)
|
||||
print e.tables
|
||||
|
||||
"""
|
||||
|
||||
from xml.sax import saxutils
|
||||
from xml.sax import parse
|
||||
|
||||
class ExcelHandler(saxutils.DefaultHandler):
|
||||
def __init__(self):
|
||||
self.chars=[]
|
||||
self.isNumber = 0
|
||||
self.cells=[]
|
||||
self.rows=[]
|
||||
self.tables=[]
|
||||
|
||||
def characters(self, content):
|
||||
self.chars.append(content)
|
||||
|
||||
def startElement(self, name, attrs):
|
||||
if name=="Data":
|
||||
if attrs.get('ss:Type') == "Number":
|
||||
self.isNumber = 1
|
||||
else:
|
||||
self.isNumber = 0
|
||||
elif name=="Cell":
|
||||
self.chars=[]
|
||||
elif name=="Row":
|
||||
self.cells=[]
|
||||
elif name=="Table":
|
||||
self.rows=[]
|
||||
|
||||
def endElement(self, name):
|
||||
if name=="Data":
|
||||
pass
|
||||
elif name=="Cell":
|
||||
s = ''.join(self.chars)
|
||||
if self.isNumber:
|
||||
# Determine if it is an int or float and use
|
||||
# return the best fit
|
||||
floatVersion = float(s)
|
||||
intVersion = int(floatVersion)
|
||||
if floatVersion == intVersion:
|
||||
# If the float is equal to the int, it must be an int
|
||||
s = intVersion
|
||||
else:
|
||||
# Keep the precision and return a float
|
||||
s = floatVersion
|
||||
self.cells.append(s)
|
||||
elif name=="Row":
|
||||
self.rows.append(self.cells)
|
||||
elif name=="Table":
|
||||
self.tables.append(self.rows)
|
Loading…
x
Reference in New Issue
Block a user