Python,PygamUnicodeDecodeError?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (62)
# 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

这是我运行代码时的错误:

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)

我尝试过多种格式的编码和解码,但问题仍然存在。有什么我能做的吗?

提问于
用户回答回答于
for line in read:
    for m in line.decode(ENCODING):
        ...

扫码关注云+社区

领取腾讯云代金券