Skip to content
  • Search
Skins
  • Light
  • Brite
  • 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 1.1k Views 1 Watching
  • 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

    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
    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