From 79a5ece62ecfad5fb64da42c54ad110e822350d4 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Tue, 8 Feb 2022 22:54:40 -0800 Subject: [PATCH] Add --convert example to sqlite-utils insert --help, closes #404 --- docs/cli-reference.rst | 11 +++++++++++ sqlite_utils/cli.py | 12 ++++++++++++ 2 files changed, 23 insertions(+) diff --git a/docs/cli-reference.rst b/docs/cli-reference.rst index 15b7b8c..5b7c6fe 100644 --- a/docs/cli-reference.rst +++ b/docs/cli-reference.rst @@ -208,6 +208,17 @@ See :ref:`cli_inserting_data`, :ref:`cli_insert_csv_tsv`. Your Python code will be passed a "row" variable representing the imported row, and can return a modified row. + This example uses just the name, latitude and longitude columns from a CSV + file, converting name to upper case and latitude and longitude to floating + point numbers: + + sqlite-utils insert plants.db plants plants.csv --csv --convert ' + return { + "name": row["name"].upper(), + "latitude": float(row["latitude"]), + "longitude": float(row["longitude"]), + }' + If you are using --lines your code will be passed a "line" variable, and for --text an "text" variable. diff --git a/sqlite_utils/cli.py b/sqlite_utils/cli.py index b137d9b..2ad7aff 100644 --- a/sqlite_utils/cli.py +++ b/sqlite_utils/cli.py @@ -1144,6 +1144,18 @@ def insert( Your Python code will be passed a "row" variable representing the imported row, and can return a modified row. + This example uses just the name, latitude and longitude columns from + a CSV file, converting name to upper case and latitude and longitude + to floating point numbers: + + \b + sqlite-utils insert plants.db plants plants.csv --csv --convert ' + return { + "name": row["name"].upper(), + "latitude": float(row["latitude"]), + "longitude": float(row["longitude"]), + }' + If you are using --lines your code will be passed a "line" variable, and for --text an "text" variable. """