def to_csv(self, local_path=None, temp_file_compression=None, encoding=None, errors='strict', write_header=True, csv_name=None, **csvargs): """ Outputs table to a CSV. Additional key word arguments are passed to ``csv.writer()``. So, e.g., to override the delimiter from the default CSV dialect, provide the delimiter keyword argument. .. warning:: If a file already exists at the given location, it will be overwritten. `Args:` local_path: str The path to write the csv locally. If it ends in ".gz" or ".zip", the file will be compressed. If not specified, a temporary file will be created and returned, and that file will be removed automatically when the script is done running. temp_file_compression: str If a temp file is requested (ie. no ``local_path`` is specified), the compression type for that file. Currently "None", "gzip" or "zip" are supported. If a ``local_path`` is specified, this argument is ignored. encoding: str The CSV encoding type for `csv.writer() <https://docs.python.org/2/library/csv.html#csv.writer/>`_ errors: str Raise an Error if encountered write_header: boolean Include header in output csv_name: str If ``zip`` compression (either specified or inferred), the name of csv file within the archive. \**csvargs: kwargs ``csv_writer`` optional arguments `Returns:` str The path of the new file """ # noqa: W605 # If a zip archive. if files.zip_check(local_path, temp_file_compression): return self.to_zip_csv(archive_path=local_path, encoding=encoding, errors=errors, write_header=write_header, csv_name=csv_name, **csvargs) if not local_path: suffix = '.csv' + files.suffix_for_compression_type(temp_file_compression) local_path = files.create_temp_file(suffix=suffix) # Create normal csv/.gzip petl.tocsv(self.table, source=local_path, encoding=encoding, errors=errors, write_header=write_header, **csvargs) return local_path
def to_json(self, local_path=None, temp_file_compression=None, line_delimited=False): """ Outputs table to a JSON file .. warning:: If a file already exists at the given location, it will be overwritten. `Args:` local_path: str The path to write the JSON locally. If it ends in ".gz", it will be compressed first. If not specified, a temporary file will be created and returned, and that file will be removed automatically when the script is done running. temp_file_compression: str If a temp file is requested (ie. no ``local_path`` is specified), the compression type for that file. Currently "None" and "gzip" are supported. If a ``local_path`` is specified, this argument is ignored. line_delimited: bool Whether the file will be line-delimited JSON (with a row on each line), or a proper JSON file. `Returns:` str The path of the new file """ if not local_path: suffix = '.json' + files.suffix_for_compression_type(temp_file_compression) local_path = files.create_temp_file(suffix=suffix) # Note we don't use the much simpler petl.tojson(), since that method reads the whole # table into memory before writing to file. if files.is_gzip_path(local_path): open_fn = gzip.open mode = 'w+t' else: open_fn = open mode = 'w' with open_fn(local_path, mode) as file: if not line_delimited: file.write('[') i = 0 for row in self: if i: if not line_delimited: file.write(',') file.write('\n') i += 1 json.dump(row, file) if not line_delimited: file.write(']') return local_path
def test_suffix_for_compression_type(): assert files.suffix_for_compression_type(None) == '' assert files.suffix_for_compression_type('') == '' assert files.suffix_for_compression_type('gzip') == '.gz'