Пример #1
0
    def analyseFile(self, fileName, encoding=None, dialect_suggestion=None, limit=None):
        if encoding is None:
            encoding = "utf8"
        if self.DEBUG_PARSER:
            self.registerMessage("Analysing file: {0}, encoding: {1}".format(fileName, encoding))
        with open(fileName, 'rbU') as byte_file_obj:
            # I can't imagine this having any problems
            byte_sample = byte_file_obj.read(1000)
            byte_file_obj.seek(0)

            if dialect_suggestion:
                csvdialect = UnicodeCsvDialectUtils.get_dialect_from_suggestion(dialect_suggestion)
            else:
                try:
                    csvdialect = unicodecsv.Sniffer().sniff(byte_sample)
                    assert csvdialect.delimiter ==',', "sanity test"
                except Exception:
                    csvdialect = UnicodeCsvDialectUtils.default_dialect

            if self.DEBUG_PARSER:
                self.registerMessage(UnicodeCsvDialectUtils.dialect_to_str(csvdialect))

            unicodecsvreader = unicodecsv.reader(byte_file_obj, dialect=csvdialect, encoding=encoding, strict=True)
            return self.analyseRows(unicodecsvreader, fileName)
        return None
Пример #2
0
 def exportItems(self, filePath, colNames, dialect = None, encoding="utf8"):
     assert filePath, "needs a filepath"
     assert colNames, "needs colNames"
     assert self.objects, "meeds items"
     with open(filePath, 'w+') as outFile:
         if dialect is None:
             csvdialect = UnicodeCsvDialectUtils.act_out
         else:
             csvdialect = UnicodeCsvDialectUtils.get_dialect_from_suggestion(dialect)
         # unicodecsv.register_dialect('act_out', delimiter=',', quoting=unicodecsv.QUOTE_ALL, doublequote=False, strict=True, quotechar="\"", escapechar="`")
         if self.DEBUG_ABSTRACT:
             self.registerMessage(UnicodeCsvDialectUtils.dialect_to_str(csvdialect))
         dictwriter = unicodecsv.DictWriter(
             outFile,
             dialect=csvdialect,
             fieldnames=colNames.keys(),
             encoding=encoding,
             extrasaction='ignore',
         )
         dictwriter.writerow(colNames)
         dictwriter.writerows(self.objects)
     self.registerMessage("WROTE FILE: %s" % filePath)