def handle(self): verbose = self.io.verbosity > 0 path = self.argument("path") output = self.option("output") encoding = self.option("encoding") num_chars = parse_int(self.option("num-chars"), "num-chars") dialect = detect_dialect(path, num_chars=num_chars, encoding=encoding, verbose=verbose) if dialect is None: return self.line("Dialect detection failed.") out = (io.StringIO(newline="") if output is None else open( output, "w", newline="", encoding=encoding)) if self.option("transpose"): with open(path, "r", newline="", encoding=encoding) as fp: read = reader(fp, dialect=dialect) rows = list(read) rows = list(map(list, zip(*rows))) write = writer(out, dialect="excel") for row in rows: write.writerow(row) else: with open(path, "r", newline="", encoding=encoding) as fp: read = reader(fp, dialect=dialect) write = writer(out, dialect="excel") for row in read: write.writerow(row) if output is None: self.overwrite(out.getvalue()) out.close()
def handle(self): filename = self.argument("path") encoding = self.option("encoding") or get_encoding(filename) num_chars = parse_int(self.option("num-chars"), "num-chars") dialect = detect_dialect( filename, num_chars=num_chars, encoding=encoding, verbose=self.option("verbose"), ) if dialect is None: return self.line("Dialect detection failed.") code_lines = generate_code(filename, dialect, encoding, use_pandas=self.option("pandas")) console = code.InteractiveConsole() for line in code_lines: retcode = console.push(line) if retcode: self.line("An error occurred starting the interactive console. " "Printing commands instead:\n") self.line("\n".join(code_lines)) return self.line("Dropping you into an interactive shell.\n") banner = "CleverCSV has loaded the data into the variable: " banner += "df" if self.option("pandas") else "rows" console.interact(banner=banner)
def handle(self): verbose = self.io.verbosity > 0 num_chars = parse_int(self.option("num-chars"), "num-chars") dialect = detect_dialect( self.argument("path"), num_chars=num_chars, encoding=self.option("encoding"), verbose=verbose, ) if dialect is None: return self.line("Dialect detection failed.") if self.option("plain"): self.line(f"delimiter = {dialect.delimiter}".strip()) self.line(f"quotechar = {dialect.quotechar}".strip()) self.line(f"escapechar = {dialect.escapechar}".strip()) else: self.line("Detected: " + str(dialect))
def handle(self): filename = self.argument("path") encoding = self.option("encoding") or get_encoding(filename) num_chars = parse_int(self.option("num-chars"), "num-chars") dialect = detect_dialect( filename, num_chars=num_chars, encoding=encoding, verbose=self.option("verbose"), ) if dialect is None: return self.line("Dialect detection failed.") code_lines = generate_code( filename, dialect, encoding, use_pandas=self.option("pandas") ) self.line("\n".join(code_lines))
def handle_path(self, path, output, encoding=None, num_chars=None, verbose=False): encoding = encoding or get_encoding(path) dialect = detect_dialect(path, num_chars=num_chars, encoding=encoding, verbose=verbose) if dialect is None: self.line("Dialect detection failed.") return 1 if self.option("in-place"): return self._in_place(path, dialect, encoding) elif output is None: return self._to_stdout(path, dialect, encoding) return self._to_file(path, output, dialect, encoding)
def handle(self): filename = self.argument("path") encoding = self.option("encoding") or get_encoding(filename) num_chars = parse_int(self.option("num-chars"), "num-chars") dialect = detect_dialect( filename, num_chars=num_chars, encoding=encoding, verbose=self.option("verbose"), ) if dialect is None: return self.line("Dialect detection failed.") d = '"\\t"' if dialect.delimiter == "\t" else f'"{dialect.delimiter}"' q = '"%s"' % (dialect.quotechar.replace('"', '\\"')) e = repr(f"{dialect.escapechar}").replace("'", '"') base = [ "", f"# Code generated with CleverCSV version {__version__}", "", "import clevercsv", ] if self.option("pandas"): code = base + [ "", f'df = clevercsv.csv2df("{filename}", delimiter={d}, quotechar={q}, escapechar={e})', "", ] else: enc = "None" if encoding is None else f'"{encoding}"' code = base + [ "", f'with open("{filename}", "r", newline="", encoding={enc}) as fp:', f" reader = clevercsv.reader(fp, " + f"delimiter={d}, quotechar={q}, escapechar={e})", " rows = list(reader)", "", ] self.line("\n".join(code))