我如何告诉 Python sys.argv 是 Unicode? [英] How do I tell Python that sys.argv is in Unicode?
问题描述
这是一个小程序:
import sys
f = sys.argv[1]
print type(f)
print u"f=%s" % (f)
这是我运行的程序:
$ python x.py 'Recent/רשימת משתתפים.LNK'
<type 'str'>
Traceback (most recent call last):
File "x.py", line 5, in <module>
print u"f=%s" % (f)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd7 in position 7: ordinal not in range(128)
$
问题是 sys.argv[1] 认为它正在获取一个 ascii 字符串,它无法转换为 Unicode.但是我使用的是带有完整 Unicode 终端的 Mac,所以 x.py
实际上是获取一个 Unicode 字符串.我如何告诉 Python sys.argv[] 是 Unicode 而不是 Ascii?如果做不到这一点,我该如何将 ASCII(其中包含 unicode)转换为 Unicode?明显的转换不起作用.
The problem is that sys.argv[1] is thinking that it's getting an ascii string, which it can't convert to Unicode. But I'm using a Mac with a full Unicode-aware Terminal, so x.py
is actually getting a Unicode string. How do I tell Python that sys.argv[] is Unicode and not Ascii? Failing that, how do I convert ASCII (that has unicode inside it) into Unicode? The obvious conversions don't work.
推荐答案
您看到的 UnicodeDecodeError
错误是由于您混合了 Unicode 字符串 u"f=%s"
和 sys.argv[1]
字节串:
The UnicodeDecodeError
error you see is due to you're mixing the Unicode string u"f=%s"
and the sys.argv[1]
bytestring:
两个字节串:
both bytestrings:
$ python -c'import sys; print "f=%s" % (sys.argv[1],)' 'Recent/רשימת משתתפים'
这会从/向您的终端透明地传递字节.它适用于任何编码.
This passes bytes transparently from/to your terminal. It works for any encoding.
都是 Unicode:
both Unicode:
$ python -c'import sys; print u"f=%s" % (sys.argv[1].decode("utf-8"),)' 'Rec..
在这里,您应该将 'utf-8'
替换为您的终端使用的编码.如果终端不支持 Unicode,您可以在此处使用 sys.getfilesystemencoding()
.
Here you should replace 'utf-8'
by the encoding your terminal uses. You might use sys.getfilesystemencoding()
here if the terminal is not Unicode-aware.
两个命令产生相同的输出:
Both commands produce the same output:
f=Recent/רשימת משתתפים
通常,您应该尽快将您认为是文本的字节串转换为 Unicode.
In general you should convert bytestrings that you consider to be text to Unicode as soon as possible.
这篇关于我如何告诉 Python sys.argv 是 Unicode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!