forked from RoundtableHold/roundtablehold.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv_to_yaml.py
100 lines (90 loc) · 3.79 KB
/
csv_to_yaml.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# WARNING! This file will not work out of the box. It requires some manual work.
# To use it, go to google sheets, File -> Download -> Comma Separated Values
# Edit the value of `csv_files`, `yaml_file`, `title`, and `id`
# At the bottom of the page call either table() or list()
# Then edit that function. Not every sheet is the same format. You need to read and understand the function
# so you can make the appropriate changes. After you run it please check the results, they are unlikely to be correct.
import csv
import os
import re
import yaml
from yaml_format import Page, Section
csv_file = 'crystal_tears.csv'
yaml_file = 'crystal_tears.yaml'
title = 'Crystal Tears'
id = 'crystal_tears'
def to_snake_case(name):
name = "".join(name.split())
name = re.sub(r'[^\w\s]', '', name)
name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
name = re.sub('__([A-Z])', r'_\1', name)
name = re.sub('([a-z0-9])([A-Z])', r'\1_\2', name)
return name.lower()
def escape_quotes(s):
if len(s) > 0:
s = re.sub(r'"', '\\"', s)
s = s[0].upper() + s[1:]
return s
def make_table():
section_num = 0
item_num = 0
sub_item_num = 0
location = ""
with open(os.path.join('data', yaml_file), 'w') as bosses:
bosses.write('title: "' + escape_quotes(title) + '"\nid: ' + id + '\nsections:\n')
with open(csv_file, newline='') as csvfile:
spamreader = csv.reader(csvfile)
next(spamreader)
next(spamreader)
row = next(spamreader)
table_headers = []
for header in row[2:-1]:
header = header.lower()
header = '"' + header[0].upper() + header[1:] + '"'
table_headers.append(header)
last_section = ""
section_num = 0
item_num = 0
for row in spamreader:
if row[0] != last_section:
last_section = row[0]
bosses.write(' -\n')
bosses.write(' title: "' + escape_quotes(row[0]) + '"\n')
bosses.write(' id: "' + id + '_' + to_snake_case(row[0]) + '"\n')
bosses.write(' num: ' + str(section_num) + '\n')
bosses.write(' table: [' + ', '.join(table_headers) + ']\n')
bosses.write(' items:\n')
section_num += 1
item_num = 1
bosses.write(' - [' + str(item_num) + ', "", ')
bosses.write(', '.join(['"' + escape_quotes(item) + '"' for item in row[2:-1]]))
bosses.write(']\n')
item_num += 1
def make_list():
section_num = 0
item_num = 0
sub_item_num = 0
location = ""
with open(os.path.join('data', yaml_file), 'w') as bosses:
bosses.write('title: ' + title + '\nid: ' + id + '\nsections:\n')
for filename in csv_files:
section_num += 1
item_num = 1
with open(filename, newline='') as csvfile:
spamreader = csv.reader(csvfile)
next(spamreader)
next(spamreader)
next(spamreader)
bosses.write(' -\n')
bosses.write(' title: "' + row[0] + '"\n')
bosses.write(' id: ' + to_snake_case(row[0]) + '_' + id + '\n')
bosses.write(' num: ' + str(section_num) + '\n')
bosses.write(' items:\n')
for row in spamreader:
if row[0]:
bosses.write(' - "' + escape_quotes(row[0]) + '"\n')
bosses.write(' - [' + str(item_num) + ', "", ')
bosses.write('"' + escape_quotes(row[2]) + '"')
bosses.write(']\n')
item_num += 1
make_table()