python appdata环境变量中的变音符问题 [英] Problems with umlauts in python appdata environvent variable
问题描述
问题是我的用户名包含特殊字符(德国人和ue)。
我做了一个解决办法,用于Vista和Windows 7的PyQt,但它不适用于XP系统。
有人知道这些环境变量的正确编码或另一种解决方案?
正如Mike所说,您可以从 getfilesystemencoding获取系统代码页
。该编码用于将Windows的本机Unicode字符串转换为Python使用的所有C stdio函数的字节,包括使用字节串文件路径的文件系统调用,以及 os.environ
。 / p>
这意味着您将能够从 os.environ
读取非ASCII字符的字符串,直接使用它作为文件路径,没有任何特殊的编码/解码步骤。
不幸的是,如果%APPDATA%
变量包含系统代码页中不存在的Unicode字符 - 例如,如果在德语(cp1252)Windows安装中,您的路径为 C:\Documents andSettings\αβγ\ApplicationData
- 那么这些角色在你有机会使用之前就已经被破坏了。使用文件系统编码解码您使用Unicode的字节字符串在这种情况下将不会有帮助。
这是一个可以在最近的具有 ctypes
扩展名,以阅读Windows本地Unicode环境变量。
def getEnvironmentVariable(name) :
name = unicode(name)#确保字符串参数为unicode
n = ctypes.windll.kernel32.GetEnvironmentVariableW(name,None,0)
如果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
字典包含Unicode字符串直接从Windows获取,没有代码页编码,所以你不用担心这个问题。
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?
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.
这篇关于python appdata环境变量中的变音符问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!