Skip to content

Commit c109c12

Browse files
authored
Update hoi4localisationadder.py
Improve file reading (supports more encodings)
1 parent 958ecfe commit c109c12

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

python2/hoi4localisationadder.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,19 @@
3838
#############################
3939
def readfile(name):
4040
print("Reading file " + name + "...")
41-
with open(name, "r") as f:
42-
lines = f.read().splitlines()
41+
try:
42+
with open(name, "r") as f:
43+
lines = f.read().splitlines()
44+
except:
45+
try:
46+
with open(name, "r", encoding='utf-8') as f:
47+
lines = f.read().splitlines()
48+
except:
49+
try:
50+
with open(name, "r", encoding='utf-8-sig') as f:
51+
lines = f.read().splitlines()
52+
except:
53+
print("Could not read file " + name + "!")
4354
tags = collections.OrderedDict()
4455

4556
open_blocks = 0
@@ -100,8 +111,8 @@ def readfile(name):
100111
temp_line = re.sub('\s', "", temp_line)
101112
temp_line.strip()
102113
tags[temp_line] = None
103-
open_blocks += re.sub(r'\".*?\"', '', line).count('{')
104-
open_blocks -= re.sub(r'\".*?\"', '', line).count('}')
114+
open_blocks += line.count('{')
115+
open_blocks -= line.count('}')
105116

106117
print("File " + name + " read successfully!")
107118
return list(tags.keys()), (is_event_file, is_focus_file, is_idea_file, is_decision_categories_file)
@@ -120,8 +131,19 @@ def readfile(name):
120131
#if not parsed_file[1][0] and not parsed_file[1][1] and not parsed_file[1][2:]:
121132
# sys.exit("File " + args.input + " is not a valid event, national_focus or ideas file.")
122133
lines = list()
123-
with open(args.output,"r") as f:
124-
lines = f.read().splitlines()
134+
try:
135+
with open(args.output, "r") as f:
136+
lines = f.read().splitlines()
137+
except:
138+
try:
139+
with open(args.output, "r", encoding='utf-8') as f:
140+
lines = f.read().splitlines()
141+
except:
142+
try:
143+
with open(args.output, "r", encoding='utf-8-sig') as f:
144+
lines = f.read().splitlines()
145+
except:
146+
print("Could not read file " + args.output + "!")
125147
output_lines = list()
126148
if len(lines) < 1:
127149
print("Output file " + args.output + " is empty or doesn't exist, creating a new english localisation file.")

0 commit comments

Comments
 (0)