Function to Write CSV File

I’ve developed a function to write a CSV file given a dataset. Just thought I would share with the community. Forgive me if this has already been done.

def writeCSVFile(dataSet, filename=""): import fpmi if len(filename) == 0: filename = fpmi.file.saveFile("*.csv") if filename != None: rows = [] rows.append(",".join(dataSet.getColumnNames())) pyDataSet = fpmi.db.toPyDataSet(dataSet) for row in pyDataSet: rows.append(",".join(map(lambda x: "%s" % x, row))) fpmi.file.writeFile(filename, "\r\n".join(rows))
Here’s an example:

ds = fpmi.db.toDataSet(["col1", "col2", "col3", "col4"], [[1,2,3,"A"],[5,6,7,"B"], [8,9,10,"C"]]) writeCSVFile(ds, "")

1 Like

MickyBob,
I love seeing this kind of participation and generic code sharing. Establishing such a community is an invaluable resource to all.

I think you can accomplish the same with fpmi.db.exportCSV().

Your script looks great! It’s useful for you to show users how to accomplish such a thing for a few reasons:

  1. Better understanding of how to use scripting
  2. That script can be modified/customized to conditionally change the output - our built in scripts are less flexible in this regard.
1 Like

I’ve done some refinement on my original writeCSVFormat() function by using list comprehensions, which are supposedly a lot faster, and adding minor exception handling.

I’ve also added a helper function csvFormat() that formats an item depending on its type. This can be customized to format items anyway you might imagine. One interesting aspect of this function is the recursion for an item of ListType. I obviously didn’t cover all the types. I’ll leave adding code for any additional types up to the individual.

In order to use these, place them in a module named ‘misc’ under the ‘app’ module.

def csvFormat(item):
	import types, app
	if isinstance(item, types.NoneType):
		return '"N/A"'
	elif isinstance(item, types.IntType):
		return '%d' % item
	elif isinstance(item, types.LongType):
		return '%d' % item
	elif isinstance(item, types.FloatType):
		return '%f' % item
	elif isinstance(item, types.ComplexType):
		return '"%s"' % item
	elif isinstance(item, types.StringType):
		return '"%s"' % item
	elif isinstance(item, types.UnicodeType):
		return '"%s"' % item
	elif isinstance(item, types.ListType):
		return ','.join([app.misc.csvFormat(x) for x in item])
	else:
		try:
			return '"%s"' % item
		except:
			return '"#Error#"'

def writeCSVFile(dataSet, filename=""):
   import fpmi, app
   if len(filename) == 0:
      filename = fpmi.file.saveFile("*.csv")
   if filename != None:
      rows = []
      rows.append(",".join([app.misc.csvFormat(col) for col in dataSet.getColumnNames()]))
      pyDataSet = fpmi.db.toPyDataSet(dataSet)
      for row in pyDataSet:
         rows.append(",".join([app.misc.csvFormat(col) for col in row]))
      try:
         fpmi.file.writeFile(filename, "\r\n".join(rows))
      except:
         fpmi.gui.errorBox("An error was encountered while writing the CSV file '%s'." % filename, "Error")

Here’s an expanded example:

ds = fpmi.db.toDataSet(["col1", "col2", "col3", "col4", "col5", "col6", "col7"], [[1,2,3,"A",10.0,1+1j,['a','b']],[4,5,6,"B",21.21,2+2j,['c','d']], [7,8,9,"C",17.8,3+3j,['e','f']]])
app.misc.writeCSVFile(ds, "")
1 Like

It works beautifully. I attached it as a script (module) for easy import.
misc.py (1.29 KB)

1 Like

Very nice - thanks for posting. I’m quite partial to list comprehensions, they’re just, so… clean!

1 Like