Try utf-16 if utf-8 fails

This commit is contained in:
Dustin Pianalto 2018-05-09 15:45:16 -08:00
parent fc19bb8ad8
commit ecde215ef1

View File

@ -44,22 +44,14 @@ def rename_section(cfg, sec, sec_new):
return cfg return cfg
def process_file(in_file, file_type) -> ConfigParser: def process_file(in_file, file_type, encoding) -> ConfigParser:
with open(f'{config_dir}{bot_config_file}') as f: with open(f'{config_dir}{bot_config_file}') as f:
bot_config = json.load(f) bot_config = json.load(f)
ignore_strings = bot_config['ignore_strings'][file_type] ignore_strings = bot_config['ignore_strings'][file_type]
keep_blocks = bot_config['keep_blocks'][file_type] keep_blocks = bot_config['keep_blocks'][file_type]
lines = in_file.readlines() lines = in_file.readlines()
data = [line.decode() for line in lines] # data = [line.decode() for line in in_file]
# try: data = [line.decode(encoding=encoding) for line in in_file]
# data = [line.decode(encoding='utf-8') for line in lines]
# except UnicodeDecodeError as e:
# print(e)
# try:
# data = [line.decode(encoding='utf-16') for line in lines]
# except UnicodeDecodeError as e:
# print(e)
# return 0
clean_data = list() clean_data = list()
if ignore_strings: if ignore_strings:
@ -98,11 +90,11 @@ def process_files(z) -> (ConfigParser, ConfigParser, list):
if filename.lower() == 'game.ini': if filename.lower() == 'game.ini':
# Clean the Game.ini file, removing unnecessary lines # Clean the Game.ini file, removing unnecessary lines
try: try:
game_config = process_file(z.open(filename, encoding='utf-8'), 'game.ini') game_config = process_file(z.open(filename, encoding='utf-8'), 'game.ini', 'utf-8')
except UnicodeDecodeError as e: except UnicodeDecodeError as e:
print(e) print(e)
try: try:
game_config = process_file(z.open(filename, encoding='utf-16-le'), 'game.ini') game_config = process_file(z.open(filename, encoding='utf-16-le'), 'game.ini', 'utf-16-le')
except UnicodeDecodeError as e: except UnicodeDecodeError as e:
print(e) print(e)
return 0, 0, 0 return 0, 0, 0