Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:单击Package-在使用非ASCII字符时遇到问题&;命令提示符_Python_Encoding_Click_Command_Line - Fatal编程技术网

Python:单击Package-在使用非ASCII字符时遇到问题&;命令提示符

Python:单击Package-在使用非ASCII字符时遇到问题&;命令提示符,python,encoding,click,command,line,Python,Encoding,Click,Command,Line,我正在使用Click Python包编写一个命令行工具: 这是非常有用的,但我无法解决一个问题,即当我输入非ASCII字符作为命令行工具的参数时,它总是会给我那个编码错误: 是的,我知道python中的encode()和decode(),但正如您在我的代码中所看到的,我在任何地方都没有触及这个字符串 是我的控制台的错吗?我是否缺少任何设置?我正在使用Windows 7 cmd.exe,并且知道Windows喜欢自己对文件名等进行编码。我必须使用其他控制台吗?用同样的结果尝试了python 单击

我正在使用Click Python包编写一个命令行工具:

这是非常有用的,但我无法解决一个问题,即当我输入非ASCII字符作为命令行工具的参数时,它总是会给我那个编码错误:

是的,我知道python中的encode()和decode(),但正如您在我的代码中所看到的,我在任何地方都没有触及这个字符串

是我的控制台的错吗?我是否缺少任何设置?我正在使用Windows 7 cmd.exe,并且知道Windows喜欢自己对文件名等进行编码。我必须使用其他控制台吗?用同样的结果尝试了python

单击文档说明所有字符串都被视为Unicode

我非常感谢你的帮助。 安装click与pip安装click一样简单

亲切问候,

马库里翁

我的代码:

import click
from click_shell import shell
import os



@shell(prompt='Tool > ', intro='some test...', hist_file=os.path.dirname(os.path.realpath(__file__)))
def stRec():
    pass



@stRec.command()
@click.argument('name', required=True, type=click.STRING)
def set(name):
    print "nothing"



if __name__ == '__main__':
    stRec()

我发现了这一点,不得不将其添加到其他导入语句之上(所有行都很重要):

cp1252是由报告给我的编码

import locale
print locale.getpreferredencoding()
然而,当我得到我的“name”参数时,它的编码不是cp1252,我必须通过chardet lib(pip install chardet)发现它实际上是ISO-8859-9。因为我想用这个参数创建一个文件夹,所以我把它编码回cp1252:

#!/usr/bin/python -S

import sys
reload(sys)
sys.setdefaultencoding("cp1252")
import site

import click
from click_shell import shell
import os

import locale
import chardet

@shell(prompt='Tool > ', intro='some test...', hist_file=os.path.dirname(os.path.realpath(__file__)))
def stRec():
    pass



@stRec.command()
@click.argument('name', required=True, type=click.STRING)
def set(name):
    # Create folder in the folder running this script
    folderPath = os.path.join(os.path.split(os.path.abspath(__file__))[0], name)
    folderPath = folderPath.decode(str(chardet.detect(bytes(folderPath))['encoding'])).encode(locale.getpreferredencoding())
    if not os.path.exists(folderPath):
        os.makedirs(folderPath)




if __name__ == '__main__':
    stRec()

您可以在文件顶部添加
-*-编码:utf-8-*-
。这可能对你有用。不幸的是,没有,错误仍然存在
#!/usr/bin/python -S

import sys
reload(sys)
sys.setdefaultencoding("cp1252")
import site

import click
from click_shell import shell
import os

import locale
import chardet

@shell(prompt='Tool > ', intro='some test...', hist_file=os.path.dirname(os.path.realpath(__file__)))
def stRec():
    pass



@stRec.command()
@click.argument('name', required=True, type=click.STRING)
def set(name):
    # Create folder in the folder running this script
    folderPath = os.path.join(os.path.split(os.path.abspath(__file__))[0], name)
    folderPath = folderPath.decode(str(chardet.detect(bytes(folderPath))['encoding'])).encode(locale.getpreferredencoding())
    if not os.path.exists(folderPath):
        os.makedirs(folderPath)




if __name__ == '__main__':
    stRec()