Anonymous avatar Anonymous committed 6d78ae8

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

Comments (0)

Files changed (1)

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)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.