sys.argv as bytes in Python 3k

前端 未结 2 450
别跟我提以往
别跟我提以往 2020-12-10 14:33

As Python 3k introduces strict distinction between strings and bytes, command line arguments in the array sys.argv are presented as strings. Sometimes it is necessary to tre

2条回答
  •  离开以前
    2020-12-10 15:11

    You can do:

    sys.argv[1].encode() or, if you know the encoding use it as argument or call bytes(sys.argv[1], 'latin-1').

    Both should give you a byte representation of the unicode string.

    By default, Python3 uses UTF-8.

提交回复
热议问题