不要 argparse 从命令行读取 unicode 吗? [英] Don't argparse read unicode from commandline?
本文介绍了不要 argparse 从命令行读取 unicode 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
运行 Python 2.7
Running Python 2.7
执行时:
$ python client.py get_emails -a "åäö"
我明白了:
usage: client.py get_emails [-h] [-a AREA] [-t {rfc2822,plain}]
client.py get_emails: error: argument -a/--area: invalid unicode value: '\xc3\xa5\xc3\xa4\xc3\xb6'
这是我的解析器:
def _argparse():
desc = """
Simple CLI-client for...
"""
argparser = argparse.ArgumentParser(description=desc)
subparsers = argparser.add_subparsers(dest='command')
# create the parser for the "get_emails" command
parser_get_emails = subparsers.add_parser('get_emails', help=u'Get email list')
parser_get_emails.add_argument('-a', '--area', type=unicode, help='Limit to area')
parser_get_emails.add_argument('-t', '--out_type', choices=['rfc2822', 'plain'],
default='rfc2822', help='Type of output')
args = argparser.parse_args()
return args
这是否意味着我不能在 python argparse 模块中使用任何 unicode 字符?
Does this mean I can't use any unicode characters with python argparse module?
推荐答案
你可以试试
type=lambda s: unicode(s, 'utf8')
代替
type=unicode
没有编码参数 unicode() 默认为 ascii.
Without encoding argument unicode() defaults to ascii.
这篇关于不要 argparse 从命令行读取 unicode 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文