I wished in an effort to add a
--encoding approach to
sqlite-utils insert which may perhaps also very successfully be used to alternate the file encoding used to read the incoming CSV or TSV file – look sqlite-utils #182.
Proper one area: the Click file argument used to be outlined utilizing
click.File(), which has vital facets bask in automatically going thru
- for traditional input. The code looked bask in this:
@click.argument("json_file", form=click.File(), required=Lawful) # ... def uncover(json_file): # ...
click.File() takes an optionally available
encoding= parameter, but that requires you to know the file encoding upfront. In my case I wished to use the encoding optionally supplied by an
The workaround I got here up with used to be to swap to utilizing
click.File("rb"), which opened the incoming file (or stdin chase) in binary mode. I will be able to also then wrap it in a
codecs.getreader() object that can also convert those bytes right into a Python string utilizing the person-supplied encoding.
@click.argument("json_file", form=click.File("rb"), required=Lawful) @click.option("--encoding", relief="Character encoding for input, defaults to utf-8") def uncover(json_file, encoding): encoding = encoding or "utf-8" json_file = codecs.getreader(encoding)(json_file) reader = csv.reader(json_file) # ...