forked from darvasd/upload-to-zenodo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fill_template.py
48 lines (37 loc) · 1.62 KB
/
fill_template.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import sys
import csv
import os.path
import os
def fill_template(template_filename, data_filename):
# Reading the template
with open(template_filename,"r") as template_file:
template = template_file.read()
# Creating the files based on the template and the data descriptor CSV
with open(data_filename,"r", encoding='utf-8-sig') as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
output_filename = row["FILENAME"]
print("Writing %s..." % output_filename)
# Performing replacements
filled_template = template
for column in csv_reader.fieldnames:
if column != "FILENAME":
filled_template = filled_template.replace("{%s}" % column, row[column])
print((" {%s} = %s" % (column, row[column])).encode().decode('cp850'))
# Write to output file
filepath = os.path.join(os.path.dirname(data_filename), output_filename)
with open(filepath, "w", encoding='utf-8') as output_file:
output_file.write(filled_template)
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: fill_template.py <template_filename> <data_filename>")
exit()
template_filename = sys.argv[1] # e.g. "template.json"
if not os.path.isfile(template_filename):
print("Invalid template filename.")
exit()
data_filename = sys.argv[2] # e.g. "data.csv"
if not os.path.isfile(data_filename):
print("Invalid data filename.")
exit()
fill_template(template_filename, data_filename)