Commits

Daniel Benamy  committed 6d78ae8

Fixes converting excel files with numeric fields. It's quick and dirty.

  • Participants
  • Parent commits 5918394

Comments (0)

Files changed (1)

File adu/excelconvert.py

 except ImportError:
     import xml.etree.ElementTree as X
 
-from adu.objects import ADURecord
+from objects import ADURecord
+from structure import ADUFORMAT
 
-    
+
+# There are probably other numeric fields
+NUMERIC_ADU_FORMAT = [field for field in ADUFORMAT if field[3] in ('NUMBER',)]
+
+
+def strings_to_ints(adu_record):
+    """Modifies adu_record."""
+    for field in NUMERIC_ADU_FORMAT:
+        name = field[0]
+        try:
+            adu_record[name] = int(adu_record[name])
+        except KeyError:
+            pass # every record doesn't have every field
+        except TypeError, error:
+            print >> sys.stderr, "%s. Field %s, value %s." % (error, name, adu_record[name])
+
+
 def yield_converted_from_excel_xml(xmlpath, cellmap, extra=None):
     # print >> sys.stderr, cellmap, extra
     ns = '{urn:schemas-microsoft-com:office:spreadsheet}'
     for row in rows[1:]:
         valuedict = dict(zip(headernames,
                              [x.text for x in row.findall(celltag)]))
+        # print >> sys.stderr, valuedict
         renamed = dict((k, valuedict.get(v)) for k, v in cellmap.items())
+        # print >> sys.stderr, renamed
+        strings_to_ints(renamed)
+        # print >> sys.stderr, renamed
         adu = ADURecord()
         if extra:
             adu.update(extra)