Don't argparse read unicode from commandline?

前端 未结 2 516
执念已碎
执念已碎 2021-02-03 10:15

Running Python 2.7

When executing:

$ python client.py get_emails -a \"åäö\"

I get:

usage: client.py get_emails [-h] [-a         


        
相关标签:
2条回答
  • 2021-02-03 10:56

    The command-line arguments are encoded using sys.getfilesystemencoding():

    import sys
    
    def commandline_arg(bytestring):
        unicode_string = bytestring.decode(sys.getfilesystemencoding())
        return unicode_string
    
    # ...
    parser_get_emails.add_argument('-a', '--area', type=commandline_arg)
    

    Note: You don't need it in Python 3 (the arguments are already Unicode). It uses os.fsdecode() in this case because sometimes command-line arguments might be undecodable. See PEP 383 -- Non-decodable Bytes in System Character Interfaces.

    0 讨论(0)
  • 2021-02-03 10:59

    You can try

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

    instead of

    type=unicode
    

    Without encoding argument unicode() defaults to ascii.

    0 讨论(0)
提交回复
热议问题