为什么这些字母在Linux中有效而在Windows中无效,我该如何修复它?

huangapple go评论61阅读模式
英文:

Why do these letters work in Linux and not Windows, and how do I fix it?

问题

我正在翻译一个使用Pygame/Pygame GUI制作的游戏,翻译成挪威语,大部分情况下都可以正常工作,除了特殊字符!“æ”,“ø”和“å”显示为乱码,基本上看起来像AndvÅ╎rhÅ¥r。

我向某人寻求帮助,对他们来说,这些字符显示正常。

我们唯一找到的区别是我的电脑是Windows,而他们的是Linux!我只想知道为什么会出现这种情况,以及如何修复它,使翻译变得可行。

相关的代码部分:

if os.path.exists('resources/dicts/names/names.json'):
    with open('resources/dicts/names/names.json', encoding="utf-8") as read_file:
        names_dict = ujson.loads(read_file.read())

我尝试将文件设置为以utf-8编码进行读取,希望这至少会改变文件的工作方式,并希望它能够正确读取这些特殊字符。但实际上,这没有任何效果。然后,我尝试校对并重新下载文件,希望这可能是旧版本中文件交互的问题,但这也没有任何效果。

更新:所以,现在我感到有点愚蠢。其中一个解决方案奏效了,我不确定是什么解决了问题,因为直到刚才我才注意到。旧存档中将名称保存为乱码字符,所以问题已解决。

英文:

I'm working on translating a game made using Pygame/Pygame GUI into Norwegian, and it's been mostly working, except for the special letters! "æ", "ø", and "å" have been showing up as garbled gibberish, which essentially looks like AndvÃ╎rhÃ¥r

I asked for help from someone, and for them, they've been showing up just fine.

The only difference we can find is that my computer is Windows, and theirs is Linux! I essentially just want to know why this happens, and how I can fix it so it's an actually viable translation.

The coding in question:

if os.path.exists('resources/dicts/names/names.json'):
    with open('resources/dicts/names/names.json', encoding="utf-8") as read_file:
        names_dict = ujson.loads(read_file.read())

I tried setting the files to read it as utf-8, and I expected that to at least change how the file worked, and hopefully make it read the letters correctly. Instead, it did absolutely nothing. I then tried proofreading and redownloading the file after we found it worked for them, hoping it could have been a problem with how the files interacted in the old version, but that also did nothing.

UPDATE: So, now I feel rather silly. One of the solutions worked, and I'm not sure what did it, because I didn't notice until I made a new save just now. The old save had saved the names as the garbled letters, so I suppose problem solved?

答案1

得分: 1

你可以使用 locale.getpreferredencoding(False) 检查系统的默认编码是否为UTF-8。默认情况下,Python在从文件中读取和写入时会使用系统的默认编码,通常为UTF-8,但在某些计算机上可能不同。

这是 PEP-686 的链接:https://peps.python.org/pep-0686/。

英文:

Have you checked that the text has been encoded as UTF-8 as well? By default, Python will use your systems default encoding when reading and writing from a file which is usually UTF-8, however on some machines it may be different.

This is PEP-686 https://peps.python.org/pep-0686/

You can check the systems default encoding using locale.getpreferredencoding(False)

huangapple
  • 本文由 发表于 2023年3月7日 16:52:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659765.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定