]> code.communitydata.science - taguette_google_sheet_integration.git/blobdiff - taguette-export_tags_to_csv.py
added new code to support categories
[taguette_google_sheet_integration.git] / taguette-export_tags_to_csv.py
index 53a5c488be72d6efa0f798868bd5c0bc9a683e2c..8c39554ca87089566564b203dc78dad742dc22f7 100755 (executable)
@@ -1,6 +1,7 @@
 #!/usr/bin/env python3
 
 import re
+import json
 import sqlite3
 from configparser import ConfigParser
 
@@ -23,6 +24,9 @@ sql_stmt_get = "SELECT id, path, description FROM tags WHERE project_id = ?"
 #sql_stmt_get = "SELECT tags.id, tags.path, tags.description, highlights.snippet FROM highlight_tags INNER JOIN tags ON highlight_tags.tag_id = tags.id INNER JOIN highlights ON highlight_tags.highlight_id = highlights.id WHERE project_id = ?"
 cur.execute(sql_stmt_get, (project_id,))
 
+# print out a header
+print("\t".join(['id', 'axial codes', 'tags', 'category', 'description']))
+
 while True:
     row = cur.fetchone()
     if row == None:
@@ -30,13 +34,21 @@ while True:
         
     tag_id, path, description = row
 
-    m = re.match(r'^(.+)\_(.*)$', path) 
-    if m:
-        axial = m.group(1)
-        tag = m.group(2)
+    tag_match = re.match(r'^(.+)\_(.*)$', path) 
+    if tag_match:
+        axial = tag_match.group(1)
+        tag = tag_match.group(2)
     else:
         axial = ""
         tag = path
+
+    # look for extra category information stored in the description
+    cat_match = re.match('^(.*)\s*(\{(.*)\})$', description)
+    if cat_match:
+        description = cat_match.group(1)
+        category = json.loads(cat_match.group(2))["category"]
+    else:
+        category = ""
     
-    print("\t".join([str(tag_id), axial, tag, description]))
+    print("\t".join([str(tag_id), axial, tag, category, description]))
 

Community Data Science Collective || Want to submit a patch?