-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e08a8d
commit 9d3be38
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Created on Fri Apr 5 11:38:33 2024 | ||
@author: dmelvin | ||
""" | ||
|
||
import csv | ||
import os | ||
|
||
def format_triple(subject, predicate, obj): | ||
# Check if object is a URI or literal | ||
if obj.startswith("http"): | ||
return f"<{subject}> <{predicate}> <{obj}> ." | ||
else: | ||
# Specify English language tag | ||
return f"<{subject}> <{predicate}> \"{obj}\"@en ." | ||
|
||
def convert_csv_to_nt(input_csv, output_nt): | ||
with open(input_csv, 'r', newline='', encoding='utf-8') as csvfile, open(output_nt, 'w', encoding='utf-8') as ntfile: | ||
csvreader = csv.reader(csvfile) | ||
next(csvreader) # Skip the header row | ||
for row in csvreader: | ||
if len(row) >= 3: | ||
subject, predicate, obj, *_ = row | ||
nt_triple = format_triple(subject, predicate, obj) | ||
ntfile.write(nt_triple + '\n') | ||
else: | ||
print("Error: Invalid row format - should contain at least 3 columns") | ||
|
||
if __name__ == "__main__": | ||
input_csv_file = "C:/Users/dmelvin/Downloads/construct.csv" # Change this to the path of your input CSV file | ||
output_nt_file = "C:/Users/dmelvin/Documents/graphBD_test/rdf_batch/output" # Change this to the desired path of your output .nt file | ||
|
||
# Ensure the output file has the .nt extension | ||
if not output_nt_file.endswith('.nt'): | ||
output_nt_file += '.nt' | ||
|
||
convert_csv_to_nt(input_csv_file, output_nt_file) |