| | 1 | [[TOC]] |
| | 2 | = CSV Data Import from the Web2py Shell = |
| | 3 | |
| | 4 | == Importing Data == |
| | 5 | |
| | 6 | It is possible to manually import CSV files from the web2py shell (CLI = command line interpreter, a python shell with the web2py environment). |
| | 7 | |
| | 8 | Start the web2py CLI: |
| | 9 | {{{ |
| | 10 | python web2py.py -S eden -M |
| | 11 | }}} |
| | 12 | |
| | 13 | Override authorization: |
| | 14 | {{{ |
| | 15 | auth.override = True |
| | 16 | }}} |
| | 17 | |
| | 18 | Create a resource: |
| | 19 | {{{ |
| | 20 | resource = s3mgr.define_resource("project", "activity") |
| | 21 | }}} |
| | 22 | |
| | 23 | Import data: |
| | 24 | {{{ |
| | 25 | success = resource.import_xml(open("filename.csv", "rb"), # CSV format requires file handle, XML accepts filename |
| | 26 | format="csv", # "xml", "json" or "csv" |
| | 27 | stylesheet="activity.xsl", # filename (with path) is sufficient |
| | 28 | extra_data=dict(Organisation="Example.org"), # extra columns to be added to each row |
| | 29 | files={"image.png":open("image.png", "rb")), # file attachments, filenames are the keys of the dict |
| | 30 | ignore_errors=True) # skip invalid records (False rolls back the import on error) |
| | 31 | }}} |
| | 32 | |
| | 33 | See errors (error message and error tree are returned in any case, even with ignore_errors=True): |
| | 34 | {{{ |
| | 35 | print success # prints a JSON message with the error message and the JSONified error tree |
| | 36 | print resource.error # prints the error message |
| | 37 | |
| | 38 | print s3mgr.xml.tostring(resource.error_tree, pretty_print=True) # prints the error tree as XML |
| | 39 | }}} |
| | 40 | |
| | 41 | == Tools to develop and debug XSLT stylesheets == |
| | 42 | |
| | 43 | If you want to test your XSLT stylesheet, you can also use: |
| | 44 | {{{ |
| | 45 | python static/scripts/tools/csv2xml.py mycsvfile.csv mystylesheet.xsl |
| | 46 | }}} |
| | 47 | |
| | 48 | This converts the CSV file into XML, transforms it with the specified stylesheet and prints the result to the standard output. |
| | 49 | |
| | 50 | If you redirect the output into a file, you can also use this script to produce S3XML sources from CSV files (e.g. in order to test web services): |
| | 51 | {{{ |
| | 52 | python static/scripts/tools/csv2xml.py mycsvfile.csv mystylesheet.xsl > myxmlfile.xml |
| | 53 | }}} |
| | 54 | |
| | 55 | If you want to see the XML before transformation, just omit the stylesheet parameter: |
| | 56 | {{{ |
| | 57 | python static/scripts/tools/csv2xml.py mycsvfile.csv |
| | 58 | }}} |
| | 59 | |