def floatfields(gdb,fc,fromto): """gdb used as workspace, fc is the featureclass,\ from to is a dict where key = fromtextfield, value = tofloatfield""" arcpy.env.workspace(gdb) addfields = list(fromto.values()) count = col.Counter(addfields) typeerrors = col.Counter() if max(list(col.values())) > 1: print(f'fields {addfields} seems to have duplicates') raise ValueError for f in addfields: arcpy.AddField_management(fc,f,'FLOAT') for k,v in fromto: with arcpy.da.UpdateCursor(fc,[k,v]) as cursor: for row in cursor: try: row[1] = float(row[0]) except TypeError: typeerrors.update([v]) pass cursor.updateRow(row) print(f'typeerrors for each field: {typeerrors}')
def normalizeJSON(lib): un_multi(lib) lib.pop('config', None) lib.pop('keymanager', None) lib.pop('cache', None) itemIDs = {} for itemID, item in enumerate(lib['items']): itemIDs[item['itemID']] = itemID item['itemID'] = itemID item.pop('version', None) item.pop('libraryID', None) item.pop('dateAdded', None) item.pop('dateModified', None) item.pop('uniqueFields', None) item.pop('key', None) item.pop('citekey', None) item.pop('attachments', None) item.pop('collections', None) item.pop('__citekey__', None) item.pop('citationKey', None) item.pop('uri', None) item['notes'] = sorted([ html2md(note if type(note) == str else note['note']) for note in item.get('notes', []) ]) if 'note' in item: item['note'] = html2md(item['note']) item['tags'] = sorted([(tag if type(tag) == str else tag['tag']) for tag in item.get('tags', [])]) for k in list(item.keys()): v = item[k] if v is None: del item[k] if type(v) in [list, dict] and len(v) == 0: del item[k] collections = lib.get('collections', {}) while any(coll for coll in collections.values() if not coll.get('path', None)): for coll in collections.values(): if coll.get('path', None): continue if not coll.get('parent', None): coll['path'] = [coll['name']] elif collections[coll['parent']].get('path', None): coll['path'] = collections[coll['parent']]['path'] + [ coll['name'] ] for key, coll in collections.items(): coll['key'] = ' ::: '.join(coll['path']) coll.pop('path', None) coll.pop('id', None) for key, coll in collections.items(): if coll['parent']: coll['parent'] = collections[coll['parent']]['key'] coll['collections'] = [ collections[key]['key'] for key in coll['collections'] ] coll['items'] = [itemIDs[itemID] for itemID in coll['items']] lib['collections'] = {coll['key']: coll for coll in collections.values()} return strip_obj(lib)