diff options
author | Gunnar Aastrand Grimnes <gromgull@gmail.com> | 2017-02-02 15:02:48 +0100 |
---|---|---|
committer | Gunnar Aastrand Grimnes <gromgull@gmail.com> | 2017-02-02 15:02:48 +0100 |
commit | 94a437b447bd7f61b8e6acb62e70fa5100776f47 (patch) | |
tree | 70f6daf315f2aee261bb8ef5037c68db1bb49432 /rdflib/tools | |
parent | edafe0c36186159115b23781a797d9bd02c52835 (diff) | |
download | rdflib-94a437b447bd7f61b8e6acb62e70fa5100776f47.tar.gz |
default column conversions for csv2rdf
Diffstat (limited to 'rdflib/tools')
-rw-r--r-- | rdflib/tools/csv2rdf.py | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/rdflib/tools/csv2rdf.py b/rdflib/tools/csv2rdf.py index 3a81282c..0954fb02 100644 --- a/rdflib/tools/csv2rdf.py +++ b/rdflib/tools/csv2rdf.py @@ -33,6 +33,7 @@ HELP = """ csv2rdf.py \ -b <instance-base> \ -p <property-base> \ + [-D <default>] \ [-c <classname>] \ [-i <identity column(s)>] \ [-l <label columns>] \ @@ -67,12 +68,14 @@ Long options also supported: \ Long options --col0, --col1, ... can be used to specify conversion for columns. Conversions can be: - float(), int(), split(sep, [more]), uri(base, [class]), date(format) + ignore, float(), int(), split(sep, [more]), uri(base, [class]), date(format) Long options --prop0, --prop1, ... can be used to use specific properties, rather than ones auto-generated from the headers +-D sets the default conversion for columns not listed + -f says to read config from a .ini/config file - the file must contain one section called csv2rdf, with keys like the long options, i.e.: @@ -293,6 +296,7 @@ class CSV2RDF(object): self.DEFINECLASS = False self.SKIP = 0 self.DELIM = "," + self.DEFAULT = None self.COLUMNS = {} self.PROPS = {} @@ -344,7 +348,7 @@ class CSV2RDF(object): h, l = headers[i], header_labels[i] if h == "" or l == "": continue - if self.COLUMNS.get(i) == _config_ignore: + if self.COLUMNS.get(i, self.DEFAULT) == 'ignore': continue self.triple(h, RDF.type, RDF.Property) self.triple(h, RDFS.label, rdflib.Literal(toPropertyLabel(l))) @@ -373,7 +377,7 @@ class CSV2RDF(object): for i, x in enumerate(l): x = x.strip() if x != '': - if self.COLUMNS.get(i) == _config_ignore: + if self.COLUMNS.get(i, self.DEFAULT) == 'ignore': continue try: o = self.COLUMNS.get(i, rdflib.Literal)(x) @@ -422,8 +426,8 @@ def main(): opts, files = getopt.getopt( sys.argv[1:], - "hc:b:p:i:o:Cf:l:s:d:", - ["out=", "base=", "delim=", "propbase=", "class=", + "hc:b:p:i:o:Cf:l:s:d:D:", + ["out=", "base=", "delim=", "propbase=", "class=", "default=" "ident=", "label=", "skip=", "defineclass", "help"]) opts = dict(opts) @@ -453,6 +457,8 @@ def main(): csv2rdf.DELIM = v elif k == "skip": csv2rdf.SKIP = int(v) + elif k == "default": + csv2rdf.DEFAULT = column(v) elif k.startswith("col"): csv2rdf.COLUMNS[int(k[3:])] = column(v) elif k.startswith("prop"): @@ -473,6 +479,11 @@ def main(): if "--delim" in opts: csv2rdf.DELIM = opts["--delim"] + if "-D" in opts: + csv2rdf.DEFAULT = column(opts["-D"]) + if "--default" in opts: + csv2rdf.DEFAULT = column(opts["--default"]) + if "-p" in opts: csv2rdf.PROPBASE = rdflib.Namespace(opts["-p"]) if "--propbase" in opts: |