Python unicodecsv.QUOTE_MINIMAL Examples

The following are 7 code examples of unicodecsv.QUOTE_MINIMAL(). 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 unicodecsv , or try the search function .
Example #1
Source File: test.py    From pyRevit with GNU General Public License v3.0 6 votes vote down vote up
def _test_default_attrs(self, ctor, *args):
        obj = ctor(*args)
        # Check defaults
        self.assertEqual(obj.dialect.delimiter, ',')
        self.assertEqual(obj.dialect.doublequote, True)
        self.assertEqual(obj.dialect.escapechar, None)
        self.assertEqual(obj.dialect.lineterminator, "\r\n")
        self.assertEqual(obj.dialect.quotechar, '"')
        self.assertEqual(obj.dialect.quoting, csv.QUOTE_MINIMAL)
        self.assertEqual(obj.dialect.skipinitialspace, False)
        self.assertEqual(obj.dialect.strict, False)
        # Try deleting or changing attributes (they are read-only)
        self.assertRaises(py_compat_exc, delattr,
                          obj.dialect, 'delimiter')
        self.assertRaises(py_compat_exc, setattr,
                          obj.dialect, 'delimiter', ':')
        self.assertRaises(py_compat_exc, delattr,
                          obj.dialect, 'quoting')
        self.assertRaises(py_compat_exc, setattr,
                          obj.dialect, 'quoting', None) 
Example #2
Source File: test.py    From pyRevit with GNU General Public License v3.0 6 votes vote down vote up
def test_write_escape(self):
        self._write_test(['a', 1, 'p,q'], b'a,1,"p,q"',
                         escapechar='\\')
        self.assertRaises(csv.Error,
                          self._write_test,
                          ['a', 1, 'p,"q"'], b'a,1,"p,\\"q\\""',
                          escapechar=None, doublequote=False)
        self._write_test(['a', 1, 'p,"q"'], b'a,1,"p,\\"q\\""',
                         escapechar='\\', doublequote=False)
        self._write_test(['"'], b'""""',
                         escapechar='\\', quoting=csv.QUOTE_MINIMAL)
        self._write_test(['"'], b'\\"',
                         escapechar='\\', quoting=csv.QUOTE_MINIMAL,
                         doublequote=False)
        self._write_test(['"'], b'\\"',
                         escapechar='\\', quoting=csv.QUOTE_NONE)
        self._write_test(['a', 1, 'p,q'], b'a,1,p\\,q',
                         escapechar='\\', quoting=csv.QUOTE_NONE) 
Example #3
Source File: oracle_to_azure_data_lake.py    From airflow with Apache License 2.0 5 votes vote down vote up
def __init__(
            self,
            filename,
            azure_data_lake_conn_id,
            azure_data_lake_path,
            oracle_conn_id,
            sql,
            sql_params=None,
            delimiter=",",
            encoding="utf-8",
            quotechar='"',
            quoting=csv.QUOTE_MINIMAL,
            *args, **kwargs):
        super().__init__(*args, **kwargs)
        if sql_params is None:
            sql_params = {}
        self.filename = filename
        self.oracle_conn_id = oracle_conn_id
        self.sql = sql
        self.sql_params = sql_params
        self.azure_data_lake_conn_id = azure_data_lake_conn_id
        self.azure_data_lake_path = azure_data_lake_path
        self.delimiter = delimiter
        self.encoding = encoding
        self.quotechar = quotechar
        self.quoting = quoting 
Example #4
Source File: mysql_to_hive.py    From airflow with Apache License 2.0 5 votes vote down vote up
def __init__(  # pylint: disable=too-many-arguments
            self,
            sql: str,
            hive_table: str,
            create: bool = True,
            recreate: bool = False,
            partition: Optional[Dict] = None,
            delimiter: str = chr(1),
            quoting: Optional[str] = None,
            quotechar: str = '"',
            escapechar: Optional[str] = None,
            mysql_conn_id: str = 'mysql_default',
            hive_cli_conn_id: str = 'hive_cli_default',
            tblproperties: Optional[Dict] = None,
            *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self.sql = sql
        self.hive_table = hive_table
        self.partition = partition
        self.create = create
        self.recreate = recreate
        self.delimiter = str(delimiter)
        self.quoting = quoting or csv.QUOTE_MINIMAL
        self.quotechar = quotechar
        self.escapechar = escapechar
        self.mysql_conn_id = mysql_conn_id
        self.hive_cli_conn_id = hive_cli_conn_id
        self.partition = partition or {}
        self.tblproperties = tblproperties 
Example #5
Source File: xls2json_backends.py    From pyxform with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def convert_file_to_csv_string(path):
    """
    This will open a csv or xls file and return a CSV in the format:
        sheet_name1
        ,col1,col2
        ,r1c1,r1c2
        ,r2c1,r2c2
        sheet_name2
        ,col1,col2
        ,r1c1,r1c2
        ,r2c1,r2c2

    Currently, it processes csv files and xls files to ensure consistent
    csv delimiters, etc. for tests.
    """
    if path.endswith(".csv"):
        imported_sheets = csv_to_dict(path)
    else:
        imported_sheets = xls_to_dict(path)
    foo = BytesIO()
    writer = csv.writer(foo, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL)
    for sheet_name, rows in imported_sheets.items():
        writer.writerow([sheet_name])
        out_keys = []
        out_rows = []
        for row in rows:
            out_row = []
            for key in row.keys():
                if key not in out_keys:
                    out_keys.append(key)
            for out_key in out_keys:
                out_row.append(row.get(out_key, None))
            out_rows.append(out_row)
        writer.writerow([None] + out_keys)
        for out_row in out_rows:
            writer.writerow([None] + out_row)
    return foo.getvalue().decode("utf-8") 
Example #6
Source File: test.py    From pyRevit 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'], b'a,1,"p,q"')
        self.assertRaises(csv.Error,
                          self._write_test,
                          ['a', 1, 'p,q'], b'a,1,p,q',
                          quoting=csv.QUOTE_NONE)
        self._write_test(['a', 1, 'p,q'], b'a,1,"p,q"',
                         quoting=csv.QUOTE_MINIMAL)
        self._write_test(['a', 1, 'p,q'], b'"a",1,"p,q"',
                         quoting=csv.QUOTE_NONNUMERIC)
        self._write_test(['a', 1, 'p,q'], b'"a","1","p,q"',
                         quoting=csv.QUOTE_ALL)
        self._write_test(['a\nb', 1], b'"a\nb","1"',
                         quoting=csv.QUOTE_ALL) 
Example #7
Source File: __init__.py    From direct-access-py with MIT License 4 votes vote down vote up
def to_csv(self, query, path, log_progress=True, **kwargs):
        """
        Write query results to CSV. Optional keyword arguments are
        provided to the csv writer object, allowing control over
        delimiters, quoting, etc. The default is comma-separated
        with csv.QUOTE_MINIMAL

        ::

            d2 = DirectAccessV2(client_id, client_secret, api_key)
            query = d2.query('rigs', deleteddate='null', pagesize=1500)
            # Write tab-separated file
            d2.to_csv(query, '/path/to/rigs.csv', delimiter='\\t')

        :param query: DirectAccessV1 or DirectAccessV2 query object
        :param path: relative or absolute filesystem path for created CSV
        :type path: str
        :param log_progress: whether to log progress. if True, log a message with current written count
        :type log_progress: bool
        :return: the newly created CSV file path
        """
        with open(path, mode="wb") as f:
            writer = csv.writer(f, **kwargs)
            count = None
            for i, row in enumerate(query, start=1):
                row = OrderedDict(sorted(row.items(), key=lambda t: t[0]))
                count = i
                if count == 1:
                    writer.writerow(row.keys())
                writer.writerow(row.values())

                if log_progress and i % 100000 == 0:
                    self.logger.info(
                        "Wrote {count} records to file {path}".format(
                            count=count, path=path
                        )
                    )
            self.logger.info(
                "Completed writing CSV file to {path}. Final count {count}".format(
                    path=path, count=count
                )
            )
        return path