Entertainment at it's peak. The news is by your side.

Explicit file encodings using click.File


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 --encoding= option.

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)
    # ...

Created 2020-10-16T13: 39: 08-07: 00, up in the past 2020-10-16T16: 15: 49-07: 00 · Historical past · Edit

Read More

Leave A Reply

Your email address will not be published.