Skip to content
Permalink
Browse files

[processing] use csv module for table writer

  • Loading branch information
alexbruy committed Sep 5, 2013
1 parent 6689964 commit 4a6eec75fa4d3f4520780b603c7a0ace2893e210
Showing with 42 additions and 18 deletions.
  1. +42 −18 python/plugins/processing/core/TableWriter.py
@@ -23,29 +23,53 @@
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

from qgis.core import *

from PyQt4.QtCore import *
import csv
import codecs
import cStringIO

class TableWriter:

def __init__(self, fileName, encoding, fields):
self.fileName = fileName
self.writer = None
if not self.fileName.lower().endswith("csv"):
self.fileName += ".csv"

if encoding is None:
settings = QSettings()
encoding = settings.value("/ProcessingQGIS/encoding", "System")
self.encoding = encoding
if self.encoding is None or encoding == "System":
self.encoding = "utf-8"

if not fileName.endswith("csv"):
fileName += ".csv"
file = open(fileName, "w")
file.write(";".join(unicode(field.name()) for field in fields))
file.write("\n")
file.close()
with open(self.fileName, "wb") as csvFile:
self.writer = UnicodeWriter(csvFile, encoding=self.encoding)
self.writer.writerow(fields)

def addRecord(self, values):
file = open(self.fileName, "a")
file.write(";".join([unicode(value) for value in values]))
file.write("\n")
file.close()
with open(self.fileName, "ab") as csvFile:
self.writer = UnicodeWriter(csvFile, encoding=self.encoding)
self.writer.writerow(values)

def addRecords(self, records):
with open(self.fileName, "ab") as csvFile:
self.writer = UnicodeWriter(csvFile, encoding=self.encoding)
self.writer.writerows(records)

class UnicodeWriter:
def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
self.queue = cStringIO.StringIO()
self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
self.stream = f
self.encoder = codecs.getincrementalencoder(encoding)()

def writerow(self, row):
row = map(unicode, row)
try:
self.writer.writerow([s.encode("utf-8") for s in row])
except:
self.writer.writerow(row)
data = self.queue.getvalue()
data = data.decode("utf-8")
data = self.encoder.encode(data)
self.stream.write(data)
self.queue.truncate(0)

def writerows(self, rows):
for row in rows:
self.writerow(row)

0 comments on commit 4a6eec7

Please sign in to comment.
You can’t perform that action at this time.