python appdata 环境变量中的元音变音问题

发布于 2024-08-28 06:21:08 字数 169 浏览 3 评论 0原文

我找不到正确的方法来获取 python 中 appdata 路径的环境变量。

问题是我的用户名包含特殊字符(德语 ae 和 ue)。 我为 Vista 和 Windows 7 使用 PyQt 做了一个解决方法,但它不适用于 XP 系统。

有谁知道这些环境变量的正确编码或此问题的其他解决方案?

I can't find a correct way to get the environment variable for the appdata path in python.

The problem is that my user name includes special characters (the german ae and ue).
I made a workaround wit PyQt for Vista and Windows 7 but it doesn't work for XP Systems.

Does anybody know the correct encoding of these environment variables or another solution for this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素手挽清风 2024-09-04 06:21:08

正如 Mike 所说,您可以从 getfilesystemencoding 获取系统代码页。此编码用于将 Windows 的本机 Unicode 字符串转换为 Python 使用的所有 C stdio 函数的字节,包括使用字节字符串文件路径和 os.environ 的文件系统调用。

这意味着您将能够从 os.environ 读取包含非 ASCII 字符的字符串,并将其直接用作文件路径,无需任何特殊的编码/解码步骤。

不幸的是,如果 %APPDATA% 变量包含系统代码页中不存在的 Unicode 字符 - 例如,如果在德语 (cp1252) Windows 安装中,您的路径为 C:\Documents和 Settings\αβγ\Application Data - 那么这些字符在你有机会使用它们之前就已经被破坏了。在这种情况下,使用 filesystemencoding 将获得的字节字符串解码为 Unicode 将无济于事。

您可以在具有 ctypes 扩展名的最新 Python 版本上使用以下函数来读取 Windows 本机 Unicode 环境变量。

def getEnvironmentVariable(name):
    name= unicode(name) # make sure string argument is unicode
    n= ctypes.windll.kernel32.GetEnvironmentVariableW(name, None, 0)
    if n==0:
        return None
    buf= ctypes.create_unicode_buffer(u'\0'*n)
    ctypes.windll.kernel32.GetEnvironmentVariableW(name, buf, n)
    return buf.value

在 Python 3 中,os.environ 字典包含直接从 Windows 获取的 Unicode 字符串,没有代码页编码,因此您不必担心这个问题。

As Mike says, you can get the system codepage from getfilesystemencoding. This encoding is used to convert Windows's native Unicode strings into bytes for all C stdio functions used by Python, including the filesystem calls that use byte string filepaths, and os.environ.

What this means is that you will be able to read a string with non-ASCII characters from os.environ and use it directly as a filepath without any special encode/decode step.

Unfortunately, if the %APPDATA% variable contains Unicode characters that are not present in the system codepage — for example, if on a German (cp1252) Windows install, your path was C:\Documents and Settings\αβγ\Application Data — then those characters will have already been mangled before you get the chance to use them. Decoding the byte string you get to Unicode using the filesystemencoding won't help in that case.

Here's a function you can use on recent Python versions that have the ctypes extension, to read Windows native Unicode environment variables.

def getEnvironmentVariable(name):
    name= unicode(name) # make sure string argument is unicode
    n= ctypes.windll.kernel32.GetEnvironmentVariableW(name, None, 0)
    if n==0:
        return None
    buf= ctypes.create_unicode_buffer(u'\0'*n)
    ctypes.windll.kernel32.GetEnvironmentVariableW(name, buf, n)
    return buf.value

In Python 3, the os.environ dictionary contains Unicode strings taken directly from Windows with no codepage encoding, so you don't have to worry about this problem there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文