Skip to content

Commit 92fdadb

Browse files
author
Niki
committed
fix: add explicit encoding to open() calls for cross-platform portability
Python's open() uses locale-dependent encoding by default, which varies across platforms (UTF-8 on macOS/Linux, cp1252 on Windows). This causes files to be read/written incorrectly on some systems. Added encoding='utf-8' to all text-mode open() calls in: - searches/tabu_search.py - data_structures/arrays/sudoku_solver.py - strings/word_patterns.py - data_compression/huffman.py - strings/detecting_english_programmatically.py
1 parent 791deb4 commit 92fdadb

5 files changed

Lines changed: 7 additions & 7 deletions

File tree

data_compression/huffman.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def parse_file(file_path: str) -> list[Letter]:
2626
frequencies, then convert the dict into a list of Letters.
2727
"""
2828
chars: dict[str, int] = {}
29-
with open(file_path) as f:
29+
with open(file_path, encoding="utf-8") as f:
3030
while True:
3131
c = f.read(1)
3232
if not c:
@@ -78,7 +78,7 @@ def huffman(file_path: str) -> None:
7878
k: v for letter in traverse_tree(root, "") for k, v in letter.bitstring.items()
7979
}
8080
print(f"Huffman Coding of {file_path}: ")
81-
with open(file_path) as f:
81+
with open(file_path, encoding="utf-8") as f:
8282
while True:
8383
c = f.read(1)
8484
if not c:

data_structures/arrays/sudoku_solver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def unitsolved(unit):
206206

207207
def from_file(filename, sep="\n"):
208208
"Parse a file into a list of strings, separated by sep."
209-
with open(filename) as file:
209+
with open(filename, encoding="utf-8") as file:
210210
return file.read().strip().split(sep)
211211

212212

searches/tabu_search.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def generate_neighbours(path):
4949

5050
dict_of_neighbours = {}
5151

52-
with open(path) as f:
52+
with open(path, encoding="utf-8") as f:
5353
for line in f:
5454
if line.split()[0] not in dict_of_neighbours:
5555
_list = []
@@ -88,7 +88,7 @@ def generate_first_solution(path, dict_of_neighbours):
8888
will travel, if he follows the path in first_solution.
8989
"""
9090

91-
with open(path) as f:
91+
with open(path, encoding="utf-8") as f:
9292
start_node = f.read(1)
9393
end_node = start_node
9494

strings/detecting_english_programmatically.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
def load_dictionary() -> dict[str, None]:
88
path = os.path.split(os.path.realpath(__file__))
99
english_words: dict[str, None] = {}
10-
with open(path[0] + "/dictionary.txt") as dictionary_file:
10+
with open(path[0] + "/dictionary.txt", encoding="utf-8") as dictionary_file:
1111
for word in dictionary_file.read().split("\n"):
1212
english_words[word] = None
1313
return english_words

strings/word_patterns.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def get_word_pattern(word: str) -> str:
4646
import time
4747

4848
start_time = time.time()
49-
with open("dictionary.txt") as in_file:
49+
with open("dictionary.txt", encoding="utf-8") as in_file:
5050
word_list = in_file.read().splitlines()
5151

5252
all_patterns: dict = {}

0 commit comments

Comments
 (0)