Mercurial > hg > Lemuriformes
comparison lemuriformes/csv2sql.py @ 12:82cd4e0b66cf
csv2sql
| author | Jeff Hammel <k0scist@gmail.com> |
|---|---|
| date | Sun, 10 Dec 2017 15:18:00 -0800 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 11:afc259799019 | 12:82cd4e0b66cf |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 | |
| 3 """ | |
| 4 convert CSV to SQL | |
| 5 """ | |
| 6 # (whitepaper) | |
| 7 | |
| 8 import sys | |
| 9 from .cast import infer | |
| 10 from .cli import ConfigurationParser | |
| 11 from .columns import read_columns | |
| 12 from .url2sql import url2sql | |
| 13 | |
| 14 def main(args=sys.argv[1:]): | |
| 15 """CLI""" | |
| 16 | |
| 17 # parse command line | |
| 18 parser = ConfigurationParser(description=__doc__) | |
| 19 parser.add_argument('csv', | |
| 20 type=read_columns, | |
| 21 help="CSV source to read") | |
| 22 parser.add_argument('sql', | |
| 23 type=url2sql, | |
| 24 help="SQL connection URL") | |
| 25 parser.add_argument('table', # TODO: this would be nice to have as part of the URL </sugar> | |
| 26 help="SQL table to create") | |
| 27 options = parser.parse_args(args) | |
| 28 | |
| 29 # infer column types | |
| 30 columns = options.csv.keys() | |
| 31 column_types = {column: infer(options.csv[column]) | |
| 32 for column in columns} | |
| 33 | |
| 34 # cast columns | |
| 35 for column in columns: | |
| 36 _type = column_types[column] | |
| 37 options.csv[column] = [_type(v) for v in options.csv[column]] | |
| 38 | |
| 39 # TODO: | |
| 40 # - create table(s) | |
| 41 # - populate data | |
| 42 | |
| 43 | |
| 44 if __name__ == '__main__': | |
| 45 main() |
