visualdatatools.com Forum Index visualdatatools.com
Discussion for DataTank and DataGraph
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CSV table export

 
Post new topic   Reply to topic    visualdatatools.com Forum Index -> Feature Request
View previous topic :: View next topic  
Author Message
Adam



Joined: 01 Dec 2006
Posts: 61
Location: Sequim, WA

PostPosted: Mon Dec 23, 2013 9:32 pm    Post subject: CSV table export Reply with quote

It would be cool to be able to export lists of numbers and strings into a CSV file, with column headers and the ability to order the columns. The Table seems so close, but I've never been able to figure out exactly what it's good for.

I could do this easily with Python by walking the variables in an arbitrary .dtbin file, but would have to add a separate list field for the order of columns Sad. If variables are written sequentially in order that they appear in a variable selector, I might be able to use the order in the .dtbin file…but that seems dodgy.

Am I missing an easy way to do this?
Back to top
View user's profile Send private message
Adam



Joined: 01 Dec 2006
Posts: 61
Location: Sequim, WA

PostPosted: Mon Dec 23, 2013 11:07 pm    Post subject: Python solution Reply with quote

Well, here's my Python solution, after adding an ordered_variables method to my DTDataFile class. Ordering by appearance on disk appears to work…for now Smile.

Run this as an External Program on a File Name variable, and set the output IO filename to "Output.csv". Choose "Save Current Time" from the variable's context menu to create the file.

Code:

#!/usr/bin/env python

from datatank_py.DTDataFile import DTDataFile
import numpy as np
from itertools import izip_longest

def _string_value(var):
   
    if isinstance(var, basestring):
        return var
       
    if round(var) == var:
        return "%d" % (var)
   
    return str(var)

if __name__ == '__main__':
   
    with DTDataFile("Input.dtbin") as dtf:
       
        output_names = []
        for name in dtf.ordered_variable_names():
            if name.startswith("Seq_"):
                output_names.append(name[4:])
       
        # squeeze is okay to use on string lists
        output_variables = [np.squeeze(dtf[name]) for name in output_names]
        with open("Output.csv", "w") as outfile:
            outfile.write("%s\n" % (",".join(output_names)))
             
            for row in izip_longest(*output_variables, fillvalue="-"):
                outfile.write("%s\n" % (",".join([_string_value(x) for x in row])))
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    visualdatatools.com Forum Index -> Feature Request All times are GMT - 3 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group