-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmtgjson_parser.py
31 lines (26 loc) · 943 Bytes
/
mtgjson_parser.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
from jinja2 import Template
import csv
INPUT_PATH = "/Users/maxwoolf/Downloads/AllPrintingsCSVFiles/cards.csv"
OUTPUT_PATH = "/Users/maxwoolf/Downloads/AllPrintingsCSVFiles/cards_formated.csv"
TEMPLATE = Template(
"""{{ c.name }}{% if c.manaCost %} {{ c.manaCost }}{% endif %}
{{ c.type }}
{{ c.text }}{% if c.power %}
{{ c.power }}/{{ c.toughness }}{% endif %}{% if c.loyalty %}
Loyalty: {{ c.loyalty }}{% endif %}{% if c.flavorText %}
---
{{ c.flavorText }}{% endif %}
"""
)
card_dict = {}
with open(INPUT_PATH, "r", encoding="utf-8") as f:
r = csv.DictReader(f)
for card in r:
# Only parse card once per card name.
if card["name"] not in card_dict:
card_dict[card["name"]] = TEMPLATE.render(c=card)
with open(OUTPUT_PATH, "w", encoding="utf-8") as f:
w = csv.writer(f)
w.writerow(["text"])
for card in card_dict.values():
w.writerow([card.replace("[", "").replace("]", "")])