简体   繁体   中英

Python: pygame.mixer.music and unicode filenames

When you try to use pygame.mixer.music.open() with a filename string containing Unicode characters, it seems to throw a UnicodeEncodeError all the time:

File "C:\TestPlayer.py", line 43, in <module>
pygame.mixer.music.load(x)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 12-19: 
ordinal not in range(128)

(lines broken for your viewing pleasure)

I checked for x's existance using os.path.exists(x), which returned True. Am I doing something wrong? If not, is it possible to manually fix pygame's mixer (which is a .pyd file)?

I'm using Python 2.6, and Pygame 1.9.1.

I forgot to add the file I tried opening is an mp3 file, but Pygame's website/wiki states pygame.mixer.music should work with those. In fact, it does, as long as the filename only contains ASCII characters.

而不是传递文件名,而是以与Unicode兼容的方式打开文件,然后将文件对象传递给pygame.mixer.music.load

You tried

fle = open(filename, 'rb')
pygame.mixer.music.load(fle)

and

fle = open(filename, 'rb')
pygame.mixer.load(fle.read())

Or you could try, i dont know, something like

fle = open(filename, 'rb')
foo = fle.read()
pygame.mixer.load(fle.encode('ascii'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM