def write_group_file(repos, path=None): """ @param repos: List of dicts (id, level) @param path: (optional) Absolute path to group file. """ with open(path, "wb") as f: writer = fileio.csv_writer(f) for r in repos: writer.writerow(r["id"], r["level"])
def write_group_file( repos, path=None ): """ @param repos: List of dicts (id, level) @param path: (optional) Absolute path to group file. """ with open(path, 'wb') as f: writer = fileio.csv_writer(f) for r in repos: writer.writerow(r['id'], r['level'])
def export(json_paths, model, csv_path, required_only=False): """Write the specified objects' data to CSV. IMPORTANT: All objects in json_paths must have the same set of fields! TODO let user specify which fields to write TODO confirm that each identifier's class matches object_class @param json_paths: list of .json files @param model: str @param csv_path: Absolute path to CSV data file. @param required_only: boolean Only required fields. """ object_class = identifier.class_for_name( identifier.MODEL_CLASSES[model]['module'], identifier.MODEL_CLASSES[model]['class'] ) module = modules.Module(identifier.module_for_name( identifier.MODEL_REPO_MODELS[model]['module'] )) if hasattr(object_class, 'xmp') and not hasattr(object_class, 'mets'): # File or subclass json_paths = models.sort_file_paths(json_paths) else: # Entity or subclass json_paths = util.natural_sort(json_paths) json_paths_len = len(json_paths) Exporter._make_tmpdir(os.path.dirname(csv_path)) headers = module.csv_export_fields(required_only) # make sure we export 'id' if it's not in model FIELDS (ahem, files) if 'id' not in headers: headers.insert(0, 'id') with codecs.open(csv_path, 'wb', 'utf-8') as csvfile: writer = fileio.csv_writer(csvfile) # headers in first line writer.writerow(headers) for n,json_path in enumerate(json_paths): i = identifier.Identifier(json_path) logging.info('%s/%s - %s' % (n+1, json_paths_len, i.id)) obj = object_class.from_identifier(i) if obj: writer.writerow(obj.dump_csv(headers=headers)) return csv_path
def dump_csv(self): """Write terms to a CSV file. @returns: CSV formatted text """ output = StringIO.StringIO() writer = fileio.csv_writer(output) # metadata writer.writerow(['id', self.id]) writer.writerow(['title', self.title]) writer.writerow(['description', self.description]) # headers writer.writerow(CSV_HEADERS) # terms for term in self.terms(): writer.writerow(term.csv()) return output.getvalue()
def dump_csv(self): """Write terms to a CSV file. @returns: CSV formatted text """ output = io.StringIO() writer = fileio.csv_writer(output) # metadata writer.writerow(['id', self.id]) writer.writerow(['title', self.title]) writer.writerow(['description', self.description]) # headers writer.writerow(CSV_HEADERS) # terms for term in self.terms(): writer.writerow(term.csv()) return output.getvalue()