# Function to Write CSV File

**URL:** <https://forum.inductiveautomation.com/t/function-to-write-csv-file/502>\
**Category:** Archived Topics\
**Created:** [January 19, 2008, 3:51am UTC](https://forum.inductiveautomation.com/t/function-to-write-csv-file/502 "2008-01-19T03:51:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MickeyBob](https://avatars.discourse-cdn.com/v4/letter/m/e19adc/32.png) [@MickeyBob](https://forum.inductiveautomation.com/u/MickeyBob)\
**Post date:** [January 19, 2008, 3:51am UTC](https://forum.inductiveautomation.com/t/function-to-write-csv-file/502/1 "2008-01-19T03:51:54Z")

</div>

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, "")`

---

<div class="post-metadata">

**Author:** ![nathan](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@nathan](https://forum.inductiveautomation.com/u/nathan)\
**Post date:** [January 19, 2008, 5:32am UTC](https://forum.inductiveautomation.com/t/function-to-write-csv-file/502/2 "2008-01-19T05:32:39Z")

</div>

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()](http://www.inductiveautomation.com/products/factorypmi/usermanual/source/technical/jython/modulesdb.htm#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.

---

<div class="post-metadata">

**Author:** ![MickeyBob](https://avatars.discourse-cdn.com/v4/letter/m/e19adc/32.png) [@MickeyBob](https://forum.inductiveautomation.com/u/MickeyBob)\
**Post date:** [February 9, 2008, 11:41pm UTC](https://forum.inductiveautomation.com/t/function-to-write-csv-file/502/3 "2008-02-09T23:41:48Z")

</div>

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.

```auto
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:

```auto
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, "")
```

---

<div class="post-metadata">

**Author:** ![nathan](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@nathan](https://forum.inductiveautomation.com/u/nathan)\
**Post date:** [February 9, 2008, 11:52pm UTC](https://forum.inductiveautomation.com/t/function-to-write-csv-file/502/4 "2008-02-09T23:52:26Z")

</div>

It works beautifully. I attached it as a script (module) for easy import.  
[misc.py](https://us1.discourse-cdn.com/inductiveautomation/original/1X/68470d2212c4c202261ea3f9c60b14125cf12212.py) (1.29 KB)

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [February 10, 2008, 3:49am UTC](https://forum.inductiveautomation.com/t/function-to-write-csv-file/502/5 "2008-02-10T03:49:38Z")

</div>

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