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): 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))