-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_io.py
executable file
·72 lines (62 loc) · 2.01 KB
/
file_io.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
import pokemon as PK
# Read in Pokemon file
def read_pokemon_from_file(filename):
pkList = []
try:
file_in = open(filename, 'r')
except IOError:
# File not found
return
for line in file_in:
if line == "\n":
continue
# ---------- Output format ----------
# Name, Species, CP, HP, Dust, Mv1, Mv2, Appraisal, BestStat, StatLevel, IVOpts, Strengths, Weaknesses
split = line.strip().split(",")
pkmn = PK.Pokemon()
pkmn.name = split[0]
pkmn.species = split[1]
pkmn.cp = int(split[2])
pkmn.hp = int(split[3])
pkmn.dust = int(split[4])
pkmn.move_one = split[5]
pkmn.move_two = split[6]
pkmn.appraisal = int(split[7])
pkmn.bestStat = int(split[8])
pkmn.statLevel = int(split[9])
if split[10] == "":
pkmn.IVOptions = []
else:
pkmn.IVOptions = split[10].split(":")
pkmn.minIV = int(split[11])
pkmn.maxIV = int(split[12])
pkmn.skin = split[13]
# pkmn.calculate_iv_options() # Re-calculate IVs on read
pkList.append(pkmn)
file_in.close()
return pkList
def write_pokemon_to_file(pkList, filename):
try:
file_out = open(filename, 'w')
except IOError:
# File not found
return
for pkmn in pkList:
serial = ""
serial += pkmn.name + ","
serial += pkmn.species + ","
serial += str(pkmn.cp) + ","
serial += str(pkmn.hp) + ","
serial += str(pkmn.dust) + ","
serial += pkmn.move_one + ","
serial += pkmn.move_two + ","
serial += str(pkmn.appraisal) + ","
serial += str(pkmn.bestStat) + ","
serial += str(pkmn.statLevel) + ","
serial += (":".join(pkmn.IVOptions)).strip() + ","
serial += str(pkmn.minIV) + ","
serial += str(pkmn.maxIV) + ","
serial += pkmn.skin
serial = serial.strip()
file_out.write(serial+"\n")
file_out.close()