PyGame 和 Unicode - 一个永无止境的故事 [英] PyGame and Unicode - a neverending story

查看:45
本文介绍了PyGame 和 Unicode - 一个永无止境的故事的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码中做了什么:

1st 我通过使用 codecs.open 加载一个 UTF-8 文本文件(是的,双/三/四检查:它是 UTF-8)...

1st I load a UTF-8 textfile (yes, double/triple/quadruple checked: it IS UTF-8) by using with codecs.open...

def load_verbslist(folder, name, utf_encoding):
    fullname = os.path.join("daten", folder, name)
    if utf_encoding:
        with codecs.open(fullname, "r", "utf-8-sig") as name:
            lines = name.readlines()
    else:
        name = open(fullname, "r")
        lines = name.readlines()
    for x in range(0, len(lines)):
        lines[x] = lines[x].strip("\n")
        lines[x] = lines[x].strip("\r")
    return lines

我的解决方案字符串来自该文件.后来我分割线并再次编码所有内容以将其按如下方式显示到屏幕上:

From that file come my solution strings. I later split the lines and encode everything again to blit it to the screen like this:

class BlittedText():
    def __init__(self, number, colour):
        self.number = number
        self.colour = colour
        if self.number == 0: #Infinitiv
            self.content = Solution.verb[0]
            self.content = self.content.encode("utf-8")
            self.text = Main.font1.render(str(self.content), 1, self.colour)
            self.pos = (45, 45)

然后,我通过几个 BlittedText() 类附加一个名为Strings"的列表.

Then I append a list named "Strings" by several of those BlittedText() classes.

然后我将它闪烁到屏幕上:

Then I blit it to the screen:

for element in Strings:
    screen.blit(element, position)

结果可以在这张图片中看到:http://img341.imageshack.us/img341/6617/ee43.png在 Python shell(左侧)中,一切都正确显示,我的输入(最左侧)作为解决方案 TXT 文件中的字符串(当然,100% 保存为 UTF-8).在屏幕上,我的输入(黑色)正确闪烁,而解决方案字符串(绿色和红色)显示奇怪的字符而不是 unicode 字符.我想,我正确地对它们进行了编码,但显然不是:/

The result can be seen in this picture: http://img341.imageshack.us/img341/6617/ee43.png In the Python shell (left side) everything shows correctly, my inputs (very left side) as the strings from the solution TXT file (which definitely, surely, 100% is saved as UTF-8). On the screen my input (black) blits correctly, while the solution strings (green and red) show weird characters instead of the unicode characters. I thought, I properly encoded them but obviously not :/

有人发现我的错误吗?我的想法哪里出了问题?

Does anyone find my mistake? Where's my thinking gone wrong?

非常感谢!

推荐答案

您不应该在渲染之前将字符串编码为 utf-8.当您将其编码为 utf-8 时,您将生成一个带有奇怪字符的常规字符串.

You shouldn't encode the string to utf-8 before rendering. When you encode it to utf-8 you are generating a regular string with strange characters.

如果您将其编码为 latin-1,您将摆脱奇怪的字符:

If you encode it to latin-1 you get rid of the strange characters:

self.content = self.content.encode("iso-8859-1")

这篇关于PyGame 和 Unicode - 一个永无止境的故事的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆