Skip to content
  • Search
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse

ZMT zurich med tech

  1. Home
  2. Sim4Life
  3. Python API
  4. Creating a CSV file from an algorithm's output table

Creating a CSV file from an algorithm's output table

Scheduled Pinned Locked Moved Python API
pythonapitablecsv
1 Posts 1 Posters 629 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • pcrespoP Offline
    pcrespoP Offline
    pcrespo
    wrote on last edited by Sylvain
    #1

    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)
    
    

    P.

    1 Reply Last reply
    1
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Search