-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrecipeFormatter.py
59 lines (50 loc) · 1.65 KB
/
recipeFormatter.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
"""
Takes hand-written recipes of the following format
and writes new JavaScript dictionary/object syntax
for them, for use with the Cody NPC::
Fancy New Equip (equipId)
*2 Craft Item (craftItemId)
*77 Another Craft Item (anotherCraftItemId)
*9 Yet Another Craft Item (yetAnotherCraftItemId)
Another Fancy New Equip (anotherEquipId)
*50 Yet Yet Another Craft Item...
"""
lines = []
filename = input(
"Enter the filename, or relative/absolute path if " +
"not in this directory, of the *.txt file: ")
outfilename = input(
"Enter the filename, or relative/absolute path if " +
"not in this directory, of the desired output file: ")
with open(filename, "r") as f:
lines = f.readlines()
recipes = {}
setitem = 0
recipe = []
for line in lines:
if len(line) > 1:
if line[0] != " ":
setitem = int((line.split(" ")[-1]).strip("\n()"))
continue
else:
line = line.rstrip().lstrip().strip("*)")
split = line.split(" ")
quantity = int(split[0].strip("("))
itemid = int(split[-1].strip("("))
recipe.append([itemid, quantity])
else:
recipes[str(setitem)] = recipe
setitem = 0
recipe = []
if setitem != 0:
recipes[str(setitem)] = recipe
with open(outfilename, "w") as f:
out = ""
setitems = []
for key in recipes.keys():
setitems.append(key)
for i in range(len(setitems)):
out += " " + setitems[i] + ": " + str(recipes[setitems[i]])
if i < len(setitems) - 1:
out += ",\n"
f.write(out)