如何将波兰语字符作为 Python 参数输入到 CMD 中?
How to input Polish characters into CMD as a Python parameter?
我刚开始在 Python 学习编码,我有一个简单的 Python 程序 returns Cześć <input>
其中 <input>
是名称用户可以输入 CMD 作为此 Python 程序的参数。如果没有给出输入,它将 return Cześć Świat
。它工作正常,但是当我输入名称 Łukasz
时,它会从 Ł
和程序 returns Cześć Lukasz
中删除罢工而不是正确的 Cześć Łukasz
.
在 Windows CMD 中,我使用 CD 命令转到包含 Python 程序的文件夹,然后我使用以下语句执行 Python 程序:hello.py Łukasz
.
我的脚本看起来像这样(它最初来自 Google 的 Python 练习(source),我对其进行了编辑,使其适用于 [=47] 的 unicode 字符=] 2.7 版,例如还用 'cześć' 替换了 'hello'):
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
# Define a main() function that prints a little greeting.
def main():
# Get the name from the command line, using 'World' as a fallback.
if len(sys.argv) >= 2:
name = sys.argv[1].decode('cp1252')
else:
name = u'Świat'
str = u'Cześć '+name
print str.encode('utf-8')
# This is the standard boilerplate that calls the main() function.
if __name__ == '__main__':
main()
最初我用 utf-8
解码了 sys.argv[1]
,但不知何故当我使用字母 Óó
时它会抛出一个丑陋的异常(参见 this SO answer)。使用 utf-8
或 cp1252
会导致波兰语字母(例如 ĄĆĘŁŃŚŻŹ)的重音被去除,但字母 Óó
除外,它在使用 [=24 时似乎保留了重音=],因为将那个字母与 utf-8
一起使用会导致前面提到的异常。
所以我的问题是,如何从 CMD 中检索带有重音符号的完整字符串以用于我的 Python 程序?
我不会接受暗示 remove/ignore 口音的答案!
This is a known limitation of Python 2 in Windows。 sys.argv
不接受 Unicode,字符被截断为标准 ANSI 字符页。升级到 Python 3 将解决您的问题。
我刚开始在 Python 学习编码,我有一个简单的 Python 程序 returns Cześć <input>
其中 <input>
是名称用户可以输入 CMD 作为此 Python 程序的参数。如果没有给出输入,它将 return Cześć Świat
。它工作正常,但是当我输入名称 Łukasz
时,它会从 Ł
和程序 returns Cześć Lukasz
中删除罢工而不是正确的 Cześć Łukasz
.
在 Windows CMD 中,我使用 CD 命令转到包含 Python 程序的文件夹,然后我使用以下语句执行 Python 程序:hello.py Łukasz
.
我的脚本看起来像这样(它最初来自 Google 的 Python 练习(source),我对其进行了编辑,使其适用于 [=47] 的 unicode 字符=] 2.7 版,例如还用 'cześć' 替换了 'hello'):
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
# Define a main() function that prints a little greeting.
def main():
# Get the name from the command line, using 'World' as a fallback.
if len(sys.argv) >= 2:
name = sys.argv[1].decode('cp1252')
else:
name = u'Świat'
str = u'Cześć '+name
print str.encode('utf-8')
# This is the standard boilerplate that calls the main() function.
if __name__ == '__main__':
main()
最初我用 utf-8
解码了 sys.argv[1]
,但不知何故当我使用字母 Óó
时它会抛出一个丑陋的异常(参见 this SO answer)。使用 utf-8
或 cp1252
会导致波兰语字母(例如 ĄĆĘŁŃŚŻŹ)的重音被去除,但字母 Óó
除外,它在使用 [=24 时似乎保留了重音=],因为将那个字母与 utf-8
一起使用会导致前面提到的异常。
所以我的问题是,如何从 CMD 中检索带有重音符号的完整字符串以用于我的 Python 程序?
我不会接受暗示 remove/ignore 口音的答案!
This is a known limitation of Python 2 in Windows。 sys.argv
不接受 Unicode,字符被截断为标准 ANSI 字符页。升级到 Python 3 将解决您的问题。