Python csv.QUOTE_ALL Examples

The following are 30 code examples of csv.QUOTE_ALL(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module csv , or try the search function .
Example #1
Source File: main.py    From LibrERP with GNU Affero General Public License v3.0 6 votes vote down vote up
def from_data(self, fields, rows):
        fp = StringIO()
        writer = csv.writer(fp, quoting=csv.QUOTE_ALL)

        writer.writerow([name.encode('utf-8') for name in fields])

        for data in rows:
            row = []
            for d in data:
                if isinstance(d, basestring):
                    d = d.replace('\n',' ').replace('\t',' ')
                    try:
                        d = d.encode('utf-8')
                    except UnicodeError:
                        pass
                if d is False: d = None
                row.append(d)
            writer.writerow(row)

        fp.seek(0)
        data = fp.read()
        fp.close()
        return data 
Example #2
Source File: test_csv.py    From BinderFilter with MIT License 6 votes vote down vote up
def _test_dialect_attrs(self, ctor, *args):
        # Now try with dialect-derived options
        class dialect:
            delimiter='-'
            doublequote=False
            escapechar='^'
            lineterminator='$'
            quotechar='#'
            quoting=csv.QUOTE_ALL
            skipinitialspace=True
            strict=False
        args = args + (dialect,)
        obj = ctor(*args)
        self.assertEqual(obj.dialect.delimiter, '-')
        self.assertEqual(obj.dialect.doublequote, False)
        self.assertEqual(obj.dialect.escapechar, '^')
        self.assertEqual(obj.dialect.lineterminator, "$")
        self.assertEqual(obj.dialect.quotechar, '#')
        self.assertEqual(obj.dialect.quoting, csv.QUOTE_ALL)
        self.assertEqual(obj.dialect.skipinitialspace, True)
        self.assertEqual(obj.dialect.strict, False) 
Example #3
Source File: test_csv.py    From BinderFilter with MIT License 6 votes vote down vote up
def _test_arg_valid(self, ctor, arg):
        self.assertRaises(TypeError, ctor)
        self.assertRaises(TypeError, ctor, None)
        self.assertRaises(TypeError, ctor, arg, bad_attr = 0)
        self.assertRaises(TypeError, ctor, arg, delimiter = 0)
        self.assertRaises(TypeError, ctor, arg, delimiter = 'XX')
        self.assertRaises(csv.Error, ctor, arg, 'foo')
        self.assertRaises(TypeError, ctor, arg, delimiter=None)
        self.assertRaises(TypeError, ctor, arg, delimiter=1)
        self.assertRaises(TypeError, ctor, arg, quotechar=1)
        self.assertRaises(TypeError, ctor, arg, lineterminator=None)
        self.assertRaises(TypeError, ctor, arg, lineterminator=1)
        self.assertRaises(TypeError, ctor, arg, quoting=None)
        self.assertRaises(TypeError, ctor, arg,
                          quoting=csv.QUOTE_ALL, quotechar='')
        self.assertRaises(TypeError, ctor, arg,
                          quoting=csv.QUOTE_ALL, quotechar=None) 
Example #4
Source File: test_csv.py    From BinderFilter with MIT License 6 votes vote down vote up
def test_quoting(self):
        class mydialect(csv.Dialect):
            delimiter = ";"
            escapechar = '\\'
            doublequote = False
            skipinitialspace = True
            lineterminator = '\r\n'
            quoting = csv.QUOTE_NONE
        d = mydialect()

        mydialect.quoting = None
        self.assertRaises(csv.Error, mydialect)

        mydialect.doublequote = True
        mydialect.quoting = csv.QUOTE_ALL
        mydialect.quotechar = '"'
        d = mydialect()

        mydialect.quotechar = "''"
        self.assertRaises(csv.Error, mydialect)

        mydialect.quotechar = 4
        self.assertRaises(csv.Error, mydialect) 
Example #5
Source File: test_csv.py    From oss-ftp with MIT License 6 votes vote down vote up
def _test_arg_valid(self, ctor, arg):
        self.assertRaises(TypeError, ctor)
        self.assertRaises(TypeError, ctor, None)
        self.assertRaises(TypeError, ctor, arg, bad_attr = 0)
        self.assertRaises(TypeError, ctor, arg, delimiter = 0)
        self.assertRaises(TypeError, ctor, arg, delimiter = 'XX')
        self.assertRaises(csv.Error, ctor, arg, 'foo')
        self.assertRaises(TypeError, ctor, arg, delimiter=None)
        self.assertRaises(TypeError, ctor, arg, delimiter=1)
        self.assertRaises(TypeError, ctor, arg, quotechar=1)
        self.assertRaises(TypeError, ctor, arg, lineterminator=None)
        self.assertRaises(TypeError, ctor, arg, lineterminator=1)
        self.assertRaises(TypeError, ctor, arg, quoting=None)
        self.assertRaises(TypeError, ctor, arg,
                          quoting=csv.QUOTE_ALL, quotechar='')
        self.assertRaises(TypeError, ctor, arg,
                          quoting=csv.QUOTE_ALL, quotechar=None) 
Example #6
Source File: test_csv.py    From oss-ftp with MIT License 6 votes vote down vote up
def _test_dialect_attrs(self, ctor, *args):
        # Now try with dialect-derived options
        class dialect:
            delimiter='-'
            doublequote=False
            escapechar='^'
            lineterminator='$'
            quotechar='#'
            quoting=csv.QUOTE_ALL
            skipinitialspace=True
            strict=False
        args = args + (dialect,)
        obj = ctor(*args)
        self.assertEqual(obj.dialect.delimiter, '-')
        self.assertEqual(obj.dialect.doublequote, False)
        self.assertEqual(obj.dialect.escapechar, '^')
        self.assertEqual(obj.dialect.lineterminator, "$")
        self.assertEqual(obj.dialect.quotechar, '#')
        self.assertEqual(obj.dialect.quoting, csv.QUOTE_ALL)
        self.assertEqual(obj.dialect.skipinitialspace, True)
        self.assertEqual(obj.dialect.strict, False) 
Example #7
Source File: test_csv.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def _test_dialect_attrs(self, ctor, *args):
        # Now try with dialect-derived options
        class dialect:
            delimiter='-'
            doublequote=False
            escapechar='^'
            lineterminator='$'
            quotechar='#'
            quoting=csv.QUOTE_ALL
            skipinitialspace=True
            strict=False
        args = args + (dialect,)
        obj = ctor(*args)
        self.assertEqual(obj.dialect.delimiter, '-')
        self.assertEqual(obj.dialect.doublequote, False)
        self.assertEqual(obj.dialect.escapechar, '^')
        self.assertEqual(obj.dialect.lineterminator, "$")
        self.assertEqual(obj.dialect.quotechar, '#')
        self.assertEqual(obj.dialect.quoting, csv.QUOTE_ALL)
        self.assertEqual(obj.dialect.skipinitialspace, True)
        self.assertEqual(obj.dialect.strict, False) 
Example #8
Source File: test_csv.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def _test_arg_valid(self, ctor, arg):
        self.assertRaises(TypeError, ctor)
        self.assertRaises(TypeError, ctor, None)
        self.assertRaises(TypeError, ctor, arg, bad_attr = 0)
        self.assertRaises(TypeError, ctor, arg, delimiter = 0)
        self.assertRaises(TypeError, ctor, arg, delimiter = 'XX')
        self.assertRaises(csv.Error, ctor, arg, 'foo')
        self.assertRaises(TypeError, ctor, arg, delimiter=None)
        self.assertRaises(TypeError, ctor, arg, delimiter=1)
        self.assertRaises(TypeError, ctor, arg, quotechar=1)
        self.assertRaises(TypeError, ctor, arg, lineterminator=None)
        self.assertRaises(TypeError, ctor, arg, lineterminator=1)
        self.assertRaises(TypeError, ctor, arg, quoting=None)
        self.assertRaises(TypeError, ctor, arg,
                          quoting=csv.QUOTE_ALL, quotechar='')
        self.assertRaises(TypeError, ctor, arg,
                          quoting=csv.QUOTE_ALL, quotechar=None) 
Example #9
Source File: test_csv.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def _test_dialect_attrs(self, ctor, *args):
        # Now try with dialect-derived options
        class dialect:
            delimiter='-'
            doublequote=False
            escapechar='^'
            lineterminator='$'
            quotechar='#'
            quoting=csv.QUOTE_ALL
            skipinitialspace=True
            strict=False
        args = args + (dialect,)
        obj = ctor(*args)
        self.assertEqual(obj.dialect.delimiter, '-')
        self.assertEqual(obj.dialect.doublequote, False)
        self.assertEqual(obj.dialect.escapechar, '^')
        self.assertEqual(obj.dialect.lineterminator, "$")
        self.assertEqual(obj.dialect.quotechar, '#')
        self.assertEqual(obj.dialect.quoting, csv.QUOTE_ALL)
        self.assertEqual(obj.dialect.skipinitialspace, True)
        self.assertEqual(obj.dialect.strict, False) 
Example #10
Source File: export.py    From king-phisher with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def liststore_to_csv(store, target_file, columns):
	"""
	Write the contents of a :py:class:`Gtk.ListStore` to a csv file.

	:param store: The store to export the information from.
	:type store: :py:class:`Gtk.ListStore`
	:param str target_file: The destination file for the CSV data.
	:param dict columns: A dictionary mapping store column ids to the value names.
	:return: The number of rows that were written.
	:rtype: int
	"""
	target_file_h = open(target_file, 'w')
	writer = csv.writer(target_file_h, quoting=csv.QUOTE_ALL)
	rows = liststore_export(store, columns, _csv_write, (writer,))
	target_file_h.close()
	return rows 
Example #11
Source File: test_csv.py    From ironpython3 with Apache License 2.0 6 votes vote down vote up
def _test_arg_valid(self, ctor, arg):
        self.assertRaises(TypeError, ctor)
        self.assertRaises(TypeError, ctor, None)
        self.assertRaises(TypeError, ctor, arg, bad_attr = 0)
        self.assertRaises(TypeError, ctor, arg, delimiter = 0)
        self.assertRaises(TypeError, ctor, arg, delimiter = 'XX')
        self.assertRaises(csv.Error, ctor, arg, 'foo')
        self.assertRaises(TypeError, ctor, arg, delimiter=None)
        self.assertRaises(TypeError, ctor, arg, delimiter=1)
        self.assertRaises(TypeError, ctor, arg, quotechar=1)
        self.assertRaises(TypeError, ctor, arg, lineterminator=None)
        self.assertRaises(TypeError, ctor, arg, lineterminator=1)
        self.assertRaises(TypeError, ctor, arg, quoting=None)
        self.assertRaises(TypeError, ctor, arg,
                          quoting=csv.QUOTE_ALL, quotechar='')
        self.assertRaises(TypeError, ctor, arg,
                          quoting=csv.QUOTE_ALL, quotechar=None) 
Example #12
Source File: test_csv.py    From ironpython3 with Apache License 2.0 6 votes vote down vote up
def _test_dialect_attrs(self, ctor, *args):
        # Now try with dialect-derived options
        class dialect:
            delimiter='-'
            doublequote=False
            escapechar='^'
            lineterminator='$'
            quotechar='#'
            quoting=csv.QUOTE_ALL
            skipinitialspace=True
            strict=False
        args = args + (dialect,)
        obj = ctor(*args)
        self.assertEqual(obj.dialect.delimiter, '-')
        self.assertEqual(obj.dialect.doublequote, False)
        self.assertEqual(obj.dialect.escapechar, '^')
        self.assertEqual(obj.dialect.lineterminator, "$")
        self.assertEqual(obj.dialect.quotechar, '#')
        self.assertEqual(obj.dialect.quoting, csv.QUOTE_ALL)
        self.assertEqual(obj.dialect.skipinitialspace, True)
        self.assertEqual(obj.dialect.strict, False) 
Example #13
Source File: csv.py    From recon-ng-marketplace with GNU General Public License v3.0 6 votes vote down vote up
def module_run(self):
        badcharacters = ['@', '-', '=', '+']
        filename = self.options['filename']
        # codecs module not used because the csv module converts to ASCII
        with open(filename, 'w') as outfile:
            table = self.options['table']
            csvwriter = csv.writer(outfile, quoting=csv.QUOTE_ALL)
            if self.options['headers']:
                columns = [c[0] for c in self.get_columns(table)]
                csvwriter.writerow(columns)
            cnt = 0
            rows = self.query(f'SELECT * FROM "{table}" ORDER BY 1')
            for row in rows:
                row = [x if x else '' for x in row]
                if any(row):
                    cnt += 1
                    # prevent csv injection
                    sanitized_row = []
                    for cell in row:
                        if cell and cell[0] in badcharacters:
                            cell = ' '+cell
                        sanitized_row.append(cell)
                    csvwriter.writerow(sanitized_row)
        self.output(f"{cnt} records added to '{filename}'.") 
Example #14
Source File: api.py    From SMART with MIT License 6 votes vote down vote up
def download_data(request, project_pk):
    """This function gets the labeled data and makes it available for download

    Args:
        request: The POST request
        project_pk: Primary key of the project
    Returns:
        an HttpResponse containing the requested data
    """
    project = Project.objects.get(pk=project_pk)
    data, labels = get_labeled_data(project)
    data = data.to_dict("records")

    buffer = io.StringIO()
    wr = csv.DictWriter(buffer, fieldnames=['ID', 'Text', 'Label'], quoting=csv.QUOTE_ALL)
    wr.writeheader()
    wr.writerows(data)
    buffer.seek(0)
    response = HttpResponse(buffer, content_type='text/csv')
    response['Content-Disposition'] = 'attachment;'

    return response 
Example #15
Source File: test_csv.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def _test_arg_valid(self, ctor, arg):
        self.assertRaises(TypeError, ctor)
        self.assertRaises(TypeError, ctor, None)
        self.assertRaises(TypeError, ctor, arg, bad_attr = 0)
        self.assertRaises(TypeError, ctor, arg, delimiter = 0)
        self.assertRaises(TypeError, ctor, arg, delimiter = 'XX')
        self.assertRaises(csv.Error, ctor, arg, 'foo')
        self.assertRaises(TypeError, ctor, arg, delimiter=None)
        self.assertRaises(TypeError, ctor, arg, delimiter=1)
        self.assertRaises(TypeError, ctor, arg, quotechar=1)
        self.assertRaises(TypeError, ctor, arg, lineterminator=None)
        self.assertRaises(TypeError, ctor, arg, lineterminator=1)
        self.assertRaises(TypeError, ctor, arg, quoting=None)
        self.assertRaises(TypeError, ctor, arg,
                          quoting=csv.QUOTE_ALL, quotechar='')
        self.assertRaises(TypeError, ctor, arg,
                          quoting=csv.QUOTE_ALL, quotechar=None) 
Example #16
Source File: writer.py    From iTunes_Backup_Reader with MIT License 6 votes vote down vote up
def writeToCsv(backup_list, application_list, output_file, logger):

    backup_csv = output_file + "Backups.csv"
    app_csv = output_file + "Applications.csv"


    with open(backup_csv, 'w', newline='') as backup_csv_handle:
        columns = ["Device_Name", "Product_Name", "Product_Model", "Phone_Number",
                "iOS_Version", "Last_Backup_Completion",
                "Last_Backup_Write_Completed", "User_Computers", "Passcode_Set", "Encrypted",
                "GUID", "ICCID", "IMEI", "MEID", "Serial_Num",
                "Full_Backup", "Version", "iTunes_Version"]
        wr = csv.writer(backup_csv_handle, quoting=csv.QUOTE_ALL)
        wr.writerows([columns, backup_list])

    with open(app_csv, 'w', newline='') as app_csv_handle:
        columns = ["Device_Name", "Device_SN", "App_Name", "AppleID", "User_Full_Name", "Purchase_Date",
                   "Is_Possibly_Sideloaded", "App_Version", "Is_Auto_Download", "Is_Purchased_Redownload",
                   "Publisher", "Full_App_Name"]
        wr = csv.writer(app_csv_handle, quoting=csv.QUOTE_ALL)
        wr.writerow(columns)
        wr.writerows(application_list) 
Example #17
Source File: utils.py    From git-pw with MIT License 6 votes vote down vote up
def _tabulate(output, headers, fmt):
    fmt = fmt or git_config('pw.format') or 'table'

    if fmt == 'table':
        return tabulate(output, headers, tablefmt='psql')
    elif fmt == 'simple':
        return tabulate(output, headers, tablefmt='simple')
    elif fmt == 'csv':
        result = six.StringIO()
        writer = csv.writer(
            result, quoting=csv.QUOTE_ALL, lineterminator=os.linesep)
        writer.writerow([ensure_str(h) for h in headers])
        for item in output:
            writer.writerow([ensure_str(i) for i in item])
        return result.getvalue()

    print('pw.format must be one of: table, simple, csv')
    sys.exit(1) 
Example #18
Source File: conftest.py    From datacollector-tests with Apache License 2.0 5 votes vote down vote up
def get_excel_compatible_csv(data):
    content = None
    queue = io.StringIO()
    try:
        data[1][1] = data[1][1] + '\nSTR'
        writer = csv.writer(queue, dialect='excel', quotechar='"', quoting=csv.QUOTE_ALL)
        writer.writerows(data)
        content = queue.getvalue()
    finally:
        queue.close()
    return content 
Example #19
Source File: pylint_tests.py    From mysql-utilities with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, output=None):
        """Constructor.
        """
        super(CsvReporter, self).__init__(output)
        self.writer = csv.writer(self.out, quoting=csv.QUOTE_ALL)
        self.writer.writerow(
            ["module", "msg_id", "line", "col", "obj", "msg"]
        ) 
Example #20
Source File: utils.py    From VGGFace2-pytorch with MIT License 5 votes vote down vote up
def get_id_label_map(meta_file):
    N_IDENTITY = 9131  # total number of identities in VGG Face2
    N_IDENTITY_PRETRAIN = 8631  # the number of identities used in training by Caffe
    identity_list = meta_file
    df = pd.read_csv(identity_list, sep=',\s+', quoting=csv.QUOTE_ALL, encoding="utf-8")
    df["class"] = -1
    df.loc[df["Flag"] == 1, "class"] = range(N_IDENTITY_PRETRAIN)
    df.loc[df["Flag"] == 0, "class"] = range(N_IDENTITY_PRETRAIN, N_IDENTITY)
    # print(df)
    key = df["Class_ID"].values
    val = df["class"].values
    id_label_dict = dict(zip(key, val))
    return id_label_dict 
Example #21
Source File: statistic.py    From mtl with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def handle_statistic(self, epoch, logits, sess, train_input_imgs=None, train_gts=None,
                        val_input_imgs=None, val_gts=None, verbose=1):
        self.epoch_num = epoch
        if epoch % FLAGS.val_epoch == 0:
            start = time()
            val_eval_dict = self.run_evaluation('val', logits, sess, input_imgs=val_input_imgs, gts=val_gts)
            middle = time()
            print('time for val eval: %.2f' %(middle-start))
            train_eval_dict = self.run_evaluation('train', logits, sess, input_imgs=train_input_imgs, gts=train_gts)
            end = time()
            print('time for train eval: %.2f' % (end - middle))
            self.set_sigmas_and_wights()
            self.arange_eval_lists(val_eval_dict, train_eval_dict)
            self.save_plots()
            # self.save_cvss()
            if verbose:
                self.print_end_epoch_stats(train_eval_dict, val_eval_dict)

        if epoch % FLAGS.example_epoch == 0:
            start = time()
            self.calc_and_save_examples(logits, sess)
            end = time()
            print('calc example time: %.2f' % (end - start))

    # def save_csv(self):
    #     with open(os.path.join(FLAGS.stat_csv_path, 'stat.csv'), 'wb') as myfile:
    #         wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
    #         wr.writerow(mylist)
# 
Example #22
Source File: file.py    From jesse with MIT License 5 votes vote down vote up
def store_logs(export_json=False, export_tradingview=False, export_csv=False):
    # store trades
    mode = config['app']['trading_mode']

    now = str(arrow.utcnow())[0:19]
    study_name = '{}-{}'.format(mode, now).replace(":", "-")
    path = 'storage/json/{}.json'.format(study_name)
    trades_json = {'trades': []}
    for t in store.completed_trades.trades:
        trades_json['trades'].append(t.toJSON())

    if export_json:
        os.makedirs('./storage/json', exist_ok=True)
        with open(path, 'w+') as outfile:
            json.dump(trades_json, outfile)

        print('\nJSON output saved at: \n{}'.format(path))

    # store output for TradingView.com's pine-editor
    if export_tradingview:
        tradingview_logs(study_name, mode, now)

    # also write a CSV file
    if export_csv:
        path = 'storage/csv/{}.csv'.format(study_name)
        os.makedirs('./storage/csv', exist_ok=True)

        with open(path, 'w', newline='') as outfile:
            wr = csv.writer(outfile, quoting=csv.QUOTE_ALL)

            for i, t in enumerate(trades_json['trades']):
                if i == 0:
                    # header of CSV file
                    wr.writerow(t.keys())

                wr.writerow(t.values())

        print('\nCSV output saved at: \n{}'.format(path)) 
Example #23
Source File: get_hwu_data.py    From polyai-models with Apache License 2.0 5 votes vote down vote up
def _write_data_into_file(path, rows):
    with open(path, "w") as data_file:
        writer = csv.writer(data_file, quoting=csv.QUOTE_ALL)
        writer.writerows(rows) 
Example #24
Source File: cryptomatte_utilities.py    From NukeToolSet with MIT License 5 votes vote down vote up
def _decode_csv(input_str):
    """ Decodes CSVs into a list of strings. """
    import csv
    reader = csv.reader([input_str], quotechar='"', delimiter=',', escapechar="\\",
        doublequote=False, quoting=csv.QUOTE_ALL, skipinitialspace=True);
    result = []
    for row in reader:
        result += row
    return result 
Example #25
Source File: test_csv.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_quoting(self):
        class mydialect(csv.Dialect):
            delimiter = ";"
            escapechar = '\\'
            doublequote = False
            skipinitialspace = True
            lineterminator = '\r\n'
            quoting = csv.QUOTE_NONE
        d = mydialect()
        self.assertEqual(d.quoting, csv.QUOTE_NONE)

        mydialect.quoting = None
        self.assertRaises(csv.Error, mydialect)

        mydialect.doublequote = True
        mydialect.quoting = csv.QUOTE_ALL
        mydialect.quotechar = '"'
        d = mydialect()
        self.assertEqual(d.quoting, csv.QUOTE_ALL)
        self.assertEqual(d.quotechar, '"')
        self.assertTrue(d.doublequote)

        mydialect.quotechar = "''"
        with self.assertRaises(csv.Error) as cm:
            mydialect()
        self.assertEqual(str(cm.exception),
                         '"quotechar" must be a 1-character string')

        mydialect.quotechar = 4
        with self.assertRaises(csv.Error) as cm:
            mydialect()
        self.assertEqual(str(cm.exception),
                         '"quotechar" must be string, not int') 
Example #26
Source File: dump_csv.py    From opensurfaces with MIT License 5 votes vote down vote up
def handle(self, *args, **options):
        outdir = args[0] if len(args) > 0 else '.'
        for ct in ContentType.objects.all():
            model = ct.model_class()
            if ct.app_label in ['photos', 'shapes', 'mturk', 'licenses', 'contenttypes']:
                if ct.model not in ['shapeimagesample', 'pendingcontent']:
                    filename = '%s/%s.%s.csv' % (outdir, ct.app_label, ct.model)
                    print 'Dumping %s...' % filename
                    with open(filename, 'wb') as f:
                        writer = csv.writer(f, quoting=csv.QUOTE_ALL)
                        field_names = [f.name for f in model._meta.fields]
                        writer.writerow(field_names)
                        for row in model.objects.all().values_list(*field_names):
                            writer.writerow([smart_unicode(v).encode('utf-8') for v in row]) 
Example #27
Source File: dump_csv.py    From opensurfaces with MIT License 5 votes vote down vote up
def handle(self, *args, **options):
        outdir = args[0] if len(args) > 0 else '.'
        for ct in ContentType.objects.all():
            model = ct.model_class()
            if ct.app_label in ['photos', 'shapes', 'mturk', 'licenses', 'contenttypes']:
                if ct.model not in ['shapeimagesample', 'pendingcontent']:
                    filename = '%s/%s.%s.csv' % (outdir, ct.app_label, ct.model)
                    print 'Dumping %s...' % filename
                    with open(filename, 'wb') as f:
                        writer = csv.writer(f, quoting=csv.QUOTE_ALL)
                        field_names = [f.name for f in model._meta.fields]
                        writer.writerow(field_names)
                        for row in model.objects.all().values_list(*field_names):
                            writer.writerow([smart_unicode(v).encode('utf-8') for v in row]) 
Example #28
Source File: test_csv.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_write_quoting(self):
        self._write_test(['a',1,'p,q'], 'a,1,"p,q"')
        self._write_error_test(csv.Error, ['a',1,'p,q'],
                               quoting = csv.QUOTE_NONE)
        self._write_test(['a',1,'p,q'], 'a,1,"p,q"',
                         quoting = csv.QUOTE_MINIMAL)
        self._write_test(['a',1,'p,q'], '"a",1,"p,q"',
                         quoting = csv.QUOTE_NONNUMERIC)
        self._write_test(['a',1,'p,q'], '"a","1","p,q"',
                         quoting = csv.QUOTE_ALL)
        self._write_test(['a\nb',1], '"a\nb","1"',
                         quoting = csv.QUOTE_ALL) 
Example #29
Source File: upload.py    From DIVE-backend with GNU General Public License v3.0 5 votes vote down vote up
def save_json_to_csv(project_id, file_obj, file_title, file_name, file_type, path):
    f = open(path, 'rU')
    json_data = json.load(f)

    orig_type = file_name.rsplit('.', 1)[1]
    csv_file_title = file_title
    csv_file_name = csv_file_title + ".csv"
    csv_path = os.path.join(current_app.config['STORAGE_PATH'], project_id, csv_file_name)

    csv_file = open(csv_path, 'wb')
    wr = csv.writer(csv_file, quoting=csv.QUOTE_ALL)

    header = json_data[0].keys()
    num_rows = len(json_data)
    num_cols = len(header)

    if (num_rows > current_app.config['ROW_LIMIT']):
        raise UploadTooLargeException('Uploaded file has {} rows, exceeding row limit of {}'.format(num_rows, current_app.config['ROW_LIMIT']))
    elif (num_cols > current_app.config['COLUMN_LIMIT']):
        raise UploadTooLargeException('Uploaded file has {} columns, exceeding row limit of {}'.format(num_cols, current_app.config['COLUMN_LIMIT']))

    wr.writerow([v.encode('utf-8') for v in header])
    for i in range(len(json_data)) :
        row = []
        for field in header :
            row.append(json_data[i][field])
        wr.writerow([unicode(v).encode('utf-8') for v in row])
    csv_file.close()
    file_doc = {
        'title': csv_file_title,
        'file_name': csv_file_name,
        'path': csv_path,
        'type': 'csv',
        'orig_type': 'json'
    }
    return file_doc 
Example #30
Source File: utils.py    From bird_classification with MIT License 5 votes vote down vote up
def save_csv(model_pred, obj):
    with open('submission.csv',"w") as f:
        writer = csv.writer(f, delimiter=',',  quotechar='"', quoting=csv.QUOTE_ALL)
        row = ['Id', 'Category']
        writer.writerow(row)
        for i in range(len(obj.test_list)):
            row = []
            row.append(obj.test_list[i]) 
            row.append(model_pred[i]+1)
            writer.writerow(row)