Skip to content

csv2rdf not working #3074

@lalewis1

Description

@lalewis1

the csv2rdf tool appears to be in a broken state,

the below suggest changes fix 2 issues that I have identified and seems to get it working again.

issue 1: csv_reader() attempts to replace strings in a bytes object (always raises an error)
issue 2: --colx and --propx command line flags cannot be given because they are not registered with getopts

the fix for issue 2 that I have implemented, only fixes the issue for up to 100 columns, but probably this is fine. could easily be made larger.

diff --git a/rdflib/tools/csv2rdf.py b/rdflib/tools/csv2rdf.py
index d518e809..fff386f3 100644
--- a/rdflib/tools/csv2rdf.py
+++ b/rdflib/tools/csv2rdf.py
@@ -133,7 +133,7 @@ def csv_reader(csv_data, dialect=csv.excel, **kwargs):
 
 def prefixuri(x, prefix, class_: Optional[URIRef] = None):
     if prefix:
-        r = rdflib.URIRef(prefix + quote(x.encode("utf8").replace(" ", "_"), safe=""))
+        r = rdflib.URIRef(prefix + quote(x.encode("utf8").replace(b" ", b"_"), safe=""))
     else:
         r = rdflib.URIRef(x)
     uris[x] = (r, class_)
@@ -379,7 +379,7 @@ class CSV2RDF:
                         "_".join(
                             [
                                 # type error: "int" has no attribute "encode"
-                                quote(x.encode("utf8").replace(" ", "_"), safe="")  # type: ignore[attr-defined]
+                                quote(x.encode("utf8").replace(b" ", b"_"), safe="")  # type: ignore[attr-defined]
                                 # type error: Argument 2 to "index" has incompatible type "Union[Tuple[str, ...], str]"; expected "Tuple[int, ...]"
                                 for x in index(l_, self.IDENT)  # type: ignore[arg-type]
                             ]
@@ -472,7 +472,9 @@ def main():
             "skip=",
             "defineclass",
             "help",
-        ],
+        ]
+        + [f"col{i}=" for i in range(100)]
+        + [f"prop{i}=" for i in range(100)],
     )
     opts = dict(opts)
 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions