├── README.md ├── convert.py ├── LICENSE ├── convert_with_quads.py ├── my_graph.nt ├── .gitignore └── test_sheet.csv /README.md: -------------------------------------------------------------------------------- 1 | # simple-csv-to-rdf 2 | example of using RDFlib to take a CSV and make triples from it 3 | 4 | 5 | test_sheet.csv contains the data you want to turn into triples. convert.py is a script that demonstrates making a URI and literal object triple. 6 | -------------------------------------------------------------------------------- /convert.py: -------------------------------------------------------------------------------- 1 | import csv 2 | from rdflib import Graph, Literal, Namespace, URIRef 3 | from rdflib.namespace import DCTERMS, RDF, RDFS, SKOS, XSD 4 | 5 | 6 | input_file = csv.DictReader(open("test_sheet.csv")) 7 | 8 | 9 | # make a graph 10 | output_graph = Graph() 11 | 12 | 13 | for row in input_file: 14 | 15 | # convert it from an OrderedDict to a regular dict 16 | row = dict(row) 17 | 18 | #{'Subject Label': 'Pearl Wilmer Booker', 'Subject URI': 'None', 'Predicate Label': 'Daughter Of', 'Predicate URI': '', 'Predicate Symmetry': 'Asymmetric', 'Object Label': 'Mary Booker', 'Object URI': 'None'} 19 | # make a literal and add it 20 | output_graph.add( (URIRef(row['Subject URI']), RDFS.label, Literal(row['Subject Label'], lang='en')) ) 21 | 22 | # make a triple with the object as uri 23 | output_graph.add( (URIRef(row['Subject URI']), URIRef(row['Predicate URI']), URIRef(row['Object URI'])) ) 24 | 25 | 26 | 27 | output_graph.serialize(destination='my_graph.nt', format='nt') 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Semantic Lab at Pratt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /convert_with_quads.py: -------------------------------------------------------------------------------- 1 | import csv 2 | from rdflib import Graph, Literal, Namespace, URIRef 3 | from rdflib.namespace import DCTERMS, RDF, RDFS, SKOS, XSD 4 | 5 | 6 | input_file = csv.DictReader(open("test_sheet.csv")) 7 | 8 | 9 | # make a graph 10 | 11 | 12 | 13 | for row in input_file: 14 | 15 | # convert it from an OrderedDict to a regular dict 16 | row = dict(row) 17 | # print(row) 18 | #{'Subject Label': 'Pearl Wilmer Booker', 'Subject URI': 'None', 'Predicate Label': 'Daughter Of', 'Predicate URI': '', 'Predicate Symmetry': 'Asymmetric', 'Object Label': 'Mary Booker', 'Object URI': 'None'} 19 | # make a literal and add it 20 | # output_graph.add( (URIRef(row['Subject URI']), RDFS.label, Literal(row['Subject Label'], lang='en')) ) 21 | 22 | 23 | output_graph = Graph() 24 | # make a triple with the object as uri 25 | output_graph.add( (URIRef(row['Subject URI']), URIRef(row['Predicate URI']), URIRef(row['Object URI'])) ) 26 | 27 | triple = output_graph.serialize(format='nt') 28 | triple = str(triple, 'utf-8').strip() 29 | triple = triple.replace('.','') 30 | triple = f"{triple} <{row['Context']}> ." 31 | print(triple) 32 | 33 | 34 | # output_graph.serialize(destination='my_graph.nt', format='nt') 35 | -------------------------------------------------------------------------------- /my_graph.nt: -------------------------------------------------------------------------------- 1 | <> . 2 | "Lilly Brown"@en . 3 | "Milton Batiste Sr."@en . 4 | "Feliciana Mary Jolivette Batiste"@en . 5 | <> . 6 | <> . 7 | "Milton Joseph Batiste Jr."@en . 8 | "Walter Louis Batiste"@en . 9 | "Joseph Charles Jolivette"@en . 10 | "Lionel Batiste"@en . 11 | <> . 12 | <> . 13 | "Alvin Batiste"@en . 14 | <> . 15 | "Scipio Baptiste"@en . 16 | "Felicia Boykins(sp?)"@en . 17 | 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # SageMath parsed files 80 | *.sage.py 81 | 82 | # dotenv 83 | .env 84 | 85 | # virtualenv 86 | .venv 87 | venv/ 88 | ENV/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | -------------------------------------------------------------------------------- /test_sheet.csv: -------------------------------------------------------------------------------- 1 | Subject Label,Subject URI,Predicate Label,Predicate URI,Predicate Symmetry,Object Label,Object URI,Context 2 | Milton Joseph Batiste Jr.,http://id.loc.gov/authorities/names/no99007541,Son Of,,Asymmetric,Milton Batiste Sr.,None, 3 | Milton Joseph Batiste Jr.,http://id.loc.gov/authorities/names/no99007541,Son Of,,Asymmetric,Feliciana Mary Jolivette Batiste,None, 4 | Milton Batiste Sr.,None,Father Of,,Asymmetric,Milton Joseph Batiste Jr.,http://id.loc.gov/authorities/names/no99007541,http://cool.net/whatever 5 | Feliciana Mary Jolivette Batiste,None,Mother Of,,Asymmetric,Milton Joseph Batiste Jr.,http://id.loc.gov/authorities/names/no99007541, 6 | Milton Batiste Sr.,None,Spouse Of,,Symmetric,Feliciana Mary Jolivette Batiste,None, 7 | Milton Joseph Batiste Jr.,http://id.loc.gov/authorities/names/no99007541,Brother Of,,Symmetric,Maurice Scipio,None, 8 | Milton Joseph Batiste Jr.,http://id.loc.gov/authorities/names/no99007541,Brother Of,,Symmetric,Marlene,None, 9 | Milton Joseph Batiste Jr.,http://id.loc.gov/authorities/names/no99007541,Brother Of,,Symmetric,Mary,None, 10 | Milton Joseph Batiste Jr.,http://id.loc.gov/authorities/names/no99007541,Brother Of,,Symmetric,Miriam(sp?),None, 11 | Milton Joseph Batiste Jr.,http://id.loc.gov/authorities/names/no99007541,Brother Of,,Symmetric,Martha,None, 12 | Milton Joseph Batiste Jr.,http://id.loc.gov/authorities/names/no99007541,Brother Of,,Symmetric,Merial(sp?),None, 13 | Milton Batiste Sr.,None,Son Of,,Asymmetric,Scipio Baptiste,None, 14 | Milton Batiste Sr.,None,Son Of,,Asymmetric,Elizabeth Baptiste,None, 15 | Scipio Baptiste,None,Spouse Of,,Symmetric,Elizabeth Baptiste,None, 16 | Milton Batiste Sr.,None,Brother Of,,Symmetric,Felicia Boykins(sp?),None, 17 | Felicia Boykins(sp?),None,Spouse Of,,Symmetric,Earl Boykins,None, 18 | Joseph Charles Jolivette,None,Spouse Of,,Symmetric,Virginia Edwards,None, 19 | Feliciana Mary Jolivette Batiste,None,Daughter Of,,Asymmetric,Joseph Charles Jolivette,None, 20 | Feliciana Mary Jolivette Batiste,None,Daughter Of,,Asymmetric,Virginia Edwards,None, 21 | Alvin Batiste,https://www.wikidata.org/wiki/Q367071,Spouse Of,,Symmetric,Edith Chatters Batiste,https://musicbrainz.org/artist/76be3c27-b3fe-4a06-a76d-96f79663195b, 22 | Alvin Batiste,https://www.wikidata.org/wiki/Q367071,Son Of,,Asymmetric,Lilly Brown,None, 23 | Alvin Batiste,https://www.wikidata.org/wiki/Q367071,Son Of,,Asymmetric,Edgar Batiste,None, 24 | Lilly Brown,None,Spouse Of,,Symmetric,Edgar Batiste,None, 25 | Lionel Batiste,https://www.wikidata.org/wiki/Q1341386,Son Of,,Asymmetric,Walter Louis Batiste,None, 26 | Lionel Batiste,https://www.wikidata.org/wiki/Q1341386,Son Of,,Asymmetric,Irma Batiste,None, 27 | Walter Louis Batiste,None,Spouse Of,,Symmetric,Irma Batiste,None, --------------------------------------------------------------------------------