Creating a CSV file from an algorithm's output table
-
A handy snippet that dumps a TableData into a CSV file
# -*- coding: utf-8 -*- import numpy import os import s4l_v1.analysis as analysis def to_csv(file_path, table): import csv data = table.ToList() col_keys = [""] col_keys.extend( [k.replace(',', '') for k in table.ColumnKeys()] ) row_keys = [k.replace(',', '') for k in table.RowKeys()] with open(file_path, 'wt') as outcsv: writer = csv.writer(outcsv, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL, lineterminator='\n') writer.writerow(col_keys) for i, key in enumerate(row_keys): row = [key, ] row.extend(data[i]) writer.writerow(row) print "File written to {}".format(file_path) if __name__ == '__main__': # ... Assume here code to evaluate a pipeline ... table = surface_average_J_evaluator.Outputs["Surface-Average Report [ICNIRP 1998]"].Data print( "Info: table is of type {}".format( type(data) ) ) to_csv( os.path.join(basedir, "icnirp1998.csv"), table)
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login