ljson.convert package¶
Submodules¶
ljson.convert.csv module¶
Convert csv to ljson and the ohter way around.
-
ljson.convert.csv.
csv2file
(fin, fout, types=None, modifiers=None, fieldnames=None, restkey=None, restval=None, dialect='excel', **fmtargs)[source]¶ Converts the csv file to a ljson file. Reads csv from
fin
and writes tofout
.The main difference to csv2table is that csv2file uses the on-disk implementation.
This function should be used if one wants to convert just the files without using the data.
See also: csv2table
Returns: the table
-
ljson.convert.csv.
csv2table
(fin, types=None, modifiers=None, fieldnames=None, restkey=None, restval=None, dialect='excel', **fmtargs)[source]¶ Reads the csv table from
fin
and converts it to aljson.base.memTable
.The arguments fin,fieldnames,restkey,restval,dialect,fmtargs are passed to
csv.DictReader
.types
is a dict containing the types of the columns, they are mapped {<columname>:<typename>}.modifiers
is a dict containing the modifiers mapped {<columname>:[<modifier>, <modifier>]}.If a column does not have an expicite type
"str"
is used.Eg:
>>> from io import StringIO >>> fin = StringIO("id,name\n1,foo\n2,bar\n3,baz") >>> table = csv2table(fin, types = {"id": "int", "name":"str"}, modifiers = {"id": ["unique"]}) >>> list(table) [{'name': 'foo', 'id': 1}, {'name': 'bar', 'id': 2}, {'name': 'baz', 'id': 3}]
Note: to convert files csv2file should be used.
Returns: the table
-
ljson.convert.csv.
table2csv
(table, fout, restval='', extrasaction='raise', dialect='excel', *args, **kwds)[source]¶ Convert the given ljson table to csv.
The arguments
restval='', extrasaction='raise', dialect='excel', *args, **kwds
are passed tocsv.DictWriter
.Hint: Converting files can be done by using the on-disk implementation:
>>> from ljson.base.disk import Table >>> from ljson.convert.csv import table2csv >>> fin = open("test.lson", "r+") >>> table = Table.from_file(fin) >>> fout = open("test.csv", "w") >>> table2csv(table, fout)
Returns: None
ljson.convert.sql module¶
This module provides functions to convert ljson tables from/to SQL tables.
WARNING: The SQL datatype “BINARY” and the python datatype “bytes” might not work.
Note: If one wants to use json items, these items will be stored as “varchar” in SQL. The function table2sql will convert json automatically to str, but the “sql2*” functions will not convert str to json. One can convert the objects later.
-
ljson.convert.sql.
sql2file
(db, username, password, tablename, fout, host='localhost')[source]¶ Convert the SQL table to an
ljson.disk.Table
.This function should be used to read big tables.
*WARNING*:
fout
must be opened inw+
mode!See also: sql2table
Returns the resulting table.
-
ljson.convert.sql.
sql2table
(db, username, password, tablename, host='localhost')[source]¶ Convert the SQL table to an ljson table. This function(just like sql2file) does not parse json items automatically.
db, username, password and host are used to log in.
See also: sql2file
Returns the resulting table.