0
votes

Python, PyGame UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128)

I'm aware to the other answers to similar questions but none of them solved my problem.

This is my code:

# coding=utf-8
W = "─│"
ENCODING = "utf-8"

def maze():
tr_list = pygame.sprite.Group() 
count_i = 0 
count_j = 0 
f = codecs.open("files/ma.txt", mode="r+", encoding=ENCODING)  
# Open file as f
read = f.read().splitlines()
f.close()
for line in read:
    for m in line:
        if m in W:
            if m == '│':
                tr_list.add(MazeV(count_j, count_i))
            elif m == '─':
                tr_list.add(MazeH(count_j, count_i))
        count_j += ADD
    count_i += ADD
return tr_list

This is the error when I run the code:

File "/Users/user/Documents/Pact/Main.py", line 637, in <module>
main()
File "/Users/user/Documents/Pact/Main.py", line 121, in main
wall_list = maze()  # Set up the maze
File "/Users/user/Documents/Pact/Main.py", line 493, in maze
if i in WALL:  # If wall
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: 
ordinal not in range(128)

I tried encoding and decoding to many formats, but the problem keeps the same. Is there anything that I can do?

This is ma.txt:

ma.txt

Thanks in advance

1
Your error message doesn't match your code. "if i in W" does not occur int he code you've given us. Please provide the EXACT error you get when running this code, and include the entire stack trace. - Max
Please add the content of the "ma.txt" file directly to the post, otherwise we can't copy and paste it. Also, what's MazeV? - skrx
Which Python version do you use? - skrx
Python 3.6.1, MazeV is a vertical line and MazeH is horizontal. - rdo123
I can’t add ma.txt because the copy and paste destroyed it’s arrangement. - rdo123

1 Answers

0
votes

try to decode line, maybe will help

for line in read:
    for m in line.decode(ENCODING):
        ...