gpt4 book ai didi

python - 不要 argparse 从命令行读取 unicode?

转载 作者:太空狗 更新时间:2023-10-29 17:38:32 28 4
gpt4 key购买 nike

运行 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 字符?

最佳答案

你可以试试

type=lambda s: unicode(s, 'utf8')

代替

type=unicode

没有编码参数 unicode() 默认为 ascii。

关于python - 不要 argparse 从命令行读取 unicode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22947181/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com