Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

csv2rdf not working #3074

Open
lalewis1 opened this issue Feb 18, 2025 · 0 comments
Open

csv2rdf not working #3074

lalewis1 opened this issue Feb 18, 2025 · 0 comments

Comments

@lalewis1
Copy link

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)
 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant