From fffbd5548e2391b022180222f8717adcf2ff49aa Mon Sep 17 00:00:00 2001 From: Luke Campagnola <> Date: Wed, 4 Apr 2012 09:32:38 -0400 Subject: [PATCH] Added CSV exporter (only for PlotItem) --- exporters/CSVExporter.py | 61 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 exporters/CSVExporter.py diff --git a/exporters/CSVExporter.py b/exporters/CSVExporter.py new file mode 100644 index 00000000..3955174c --- /dev/null +++ b/exporters/CSVExporter.py @@ -0,0 +1,61 @@ +import pyqtgraph as pg +from pyqtgraph.Qt import QtGui, QtCore +from Exporter import Exporter +from pyqtgraph.parametertree import Parameter + + +__all__ = ['CSVExporter'] + + +class CSVExporter(Exporter): + Name = "CSV from plot data" + windows = [] + def __init__(self, item): + Exporter.__init__(self, item) + self.params = Parameter(name='params', type='group', children=[ + {'name': 'separator', 'type': 'list', 'value': 'comma', 'values': ['comma', 'tab']}, + ]) + + def parameters(self): + return self.params + + def export(self, fileName=None): + + if not isinstance(self.item, pg.PlotItem): + raise Exception("Matplotlib export currently only works with plot items") + + if fileName is None: + self.fileSaveDialog(filter=["*.csv", "*.tsv"]) + return + + fd = open(fileName, 'w') + data = [] + header = [] + for c in self.item.curves: + data.append(c.getData()) + header.extend(['x', 'y']) + + if self.params['separator'] == 'comma': + sep = ',' + else: + sep = '\t' + + fd.write(sep.join(header) + '\n') + i = 0 + while True: + done = True + for d in data: + if i < len(d[0]): + fd.write('%g%s%g%s'%(d[0][i], sep, d[1][i], sep)) + done = False + else: + fd.write(' %s %s' % (sep, sep)) + fd.write('\n') + if done: + break + i += 1 + fd.close() + + + +