Skip to content

Commit

Permalink
fix char encodings
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdavis2 committed Apr 19, 2022
1 parent 8056fc1 commit 3a3331e
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions clean_ttl_hook/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
from rdflib import Graph


def clean_ttl(input_file_path:Path):
#get a list of all leading comments in the file
def clean_ttl(input_file_path: Path):
# get a list of all leading comments in the file
comments_list = []
comment_flag = False
with open(input_file_path, "r") as f:
with open(input_file_path, "r", encoding="utf-8") as f:
for index, line in enumerate(f):
if len(line.strip()) > 0 and line.strip()[0] == '#' and index == 0:
if len(line.strip()) > 0 and line.strip()[0] == "#" and index == 0:
comments_list.append(line)
comment_flag = True

elif len(line.strip()) > 0 and line.strip()[0] == '#' and comment_flag:
elif len(line.strip()) > 0 and line.strip()[0] == "#" and comment_flag:
comments_list.append(line)

elif len(line.strip()) > 0 and line.strip()[0] != '#':
elif len(line.strip()) > 0 and line.strip()[0] != "#":
comment_flag = False

elif not comment_flag:
Expand All @@ -45,16 +45,17 @@ def clean_ttl(input_file_path:Path):

for s, p, o in g:
f.add((s, p, o))
f.serialize(destination=input_file_path, format='turtle')
f.serialize(destination=input_file_path, format="turtle")

with open(input_file_path, "r") as f:
with open(input_file_path, "r", encoding="utf-8") as f:
lines = f.readlines()

comments_list.extend(lines)
with open(input_file_path, "w") as f:
comments_list = "".join(comments_list)
f.write(comments_list)


def main():
parser = argparse.ArgumentParser()
parser.add_argument("filenames", nargs="*")
Expand All @@ -63,5 +64,6 @@ def main():
for file in args.filenames:
clean_ttl(file)


if __name__ == "__main__":
main()
main()

0 comments on commit 3a3331e

Please sign in to comment.