简体   繁体   English

Python:pygame.mixer.music和unicode文件名

[英]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: 当您尝试将pygame.mixer.music.open()与包含Unicode字符的文件名字符串一起使用时,似乎总是在抛出UnicodeEncodeError:

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. 我使用os.path.exists(x)检查了x的存在,返回了True。 Am I doing something wrong? 难道我做错了什么? If not, is it possible to manually fix pygame's mixer (which is a .pyd file)? 如果没有,是否可以手动修复pygame的混音器(.pyd文件)?

I'm using Python 2.6, and Pygame 1.9.1. 我正在使用Python 2.6和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. 我忘记添加尝试打开的文件是mp3文件,但Pygame的网站/ Wiki指出pygame.mixer.music应该可以使用这些文件。 In fact, it does, as long as the filename only contains ASCII characters. 实际上,只要文件名仅包含ASCII字符就可以。

而不是传递文件名,而是以与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'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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