csv-to-wiki/csv_to_wiki.py

47 lines
3.1 KiB
Python

import csv
import os
# Name,Given Name,Additional Name,Family Name,Yomi Name,Given Name Yomi,Additional Name Yomi,Family Name Yomi,Name Prefix,Name Suffix,Initials,Nickname,Short Name,Maiden Name,Birthday,Gender,Location,Billing Information,Directory Server,Mileage,Occupation,Hobby,Sensitivity,Priority,Subject,Notes,Language,Photo,Group Membership,E-mail 1 - Type,E-mail 1 - Value,E-mail 2 - Type,E-mail 2 - Value,E-mail 3 - Type,E-mail 3 - Value,IM 1 - Type,IM 1 - Service,IM 1 - Value,Phone 1 - Type,Phone 1 - Value,Phone 2 - Type,Phone 2 - Value,Phone 3 - Type,Phone 3 - Value,Phone 4 - Type,Phone 4 - Value,Address 1 - Type,Address 1 - Formatted,Address 1 - Street,Address 1 - City,Address 1 - PO Box,Address 1 - Region,Address 1 - Postal Code,Address 1 - Country,Address 1 - Extended Address,Organization 1 - Type,Organization 1 - Name,Organization 1 - Yomi Name,Organization 1 - Title,Organization 1 - Department,Organization 1 - Symbol,Organization 1 - Location,Organization 1 - Job Description,Relation 1 - Type,Relation 1 - Value,Relation 2 - Type,Relation 2 - Value,Relation 3 - Type,Relation 3 - Value,Relation 4 - Type,Relation 4 - Value,Relation 5 - Type,Relation 5 - Value,Relation 6 - Type,Relation 6 - Value,Relation 7 - Type,Relation 7 - Value,Relation 8 - Type,Relation 8 - Value,Relation 9 - Type,Relation 9 - Value,Relation 10 - Type,Relation 10 - Value,Relation 11 - Type,Relation 11 - Value,Website 1 - Type,Website 1 - Value,Website 2 - Type,Website 2 - Value,Website 3 - Type,Website 3 - Value,Website 4 - Type,Website 4 - Value,Website 5 - Type,Website 5 - Value,Website 6 - Type,Website 6 - Value,Website 7 - Type,Website 7 - Value,Event 1 - Type,Event 1 - Value
FILTER_KEYS = ["Given Name", "Family Name"]
OUTPUT_PATH = "/Volumes/mdrive/computer/projects/csv-to-wiki/output"
def csv_to_wiki():
print("hello")
os.system("rm -r {}/*".format(OUTPUT_PATH))
with open('contacts.csv', 'r') as csv_file:
reader = csv.DictReader(csv_file)
for row in reader:
base_name = row["Name"]
name = base_name
count = 1
new_name_found = False
while not new_name_found:
file_path = os.path.join(OUTPUT_PATH, name + ".md")
if os.path.exists(file_path):
count = count + 1
name = base_name + str(count)
else:
new_name_found = True
with open(file_path, 'w') as f:
for key, value in row.items():
if value:
if not key in FILTER_KEYS:
if key == "Group Membership":
words = list(map(lambda word: "#" + word.replace(" ", ""), value.split(":::")))
value = " ".join(words)
value = value.replace(" ::: ", "\n")
debug_line = "{}: {}".format(key, value)
output_line = "{}".format(value)
print(debug_line)
f.write(output_line + "\n")
print("*************")
if __name__ == '__main__':
csv_to_wiki()