0

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

5
  • 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. Commented Jun 11, 2018 at 20:12
  • 1
    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? Commented Jun 11, 2018 at 23:43
  • 1
    Which Python version do you use? Commented Jun 11, 2018 at 23:49
  • Python 3.6.1, MazeV is a vertical line and MazeH is horizontal. Commented Jun 12, 2018 at 8:21
  • I can’t add ma.txt because the copy and paste destroyed it’s arrangement. Commented Jun 12, 2018 at 8:23

1 Answer 1

0

try to decode line, maybe will help

for line in read:
    for m in line.decode(ENCODING):
        ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.