简体   繁体   English

SyntaxError:将字串转换为int后,语法无效

[英]SyntaxError: invalid syntax after Cast a string to int

I have written a piece of code in python and when I let it run I get the following message: 我已经用python写了一段代码,当我让它运行时,我收到以下消息:

File "port.py", line 229
    port = (int) a
                 ^
SyntaxError: invalid syntax

since the code is a little bit too long, I give only the important part in the following section: 由于代码有点太长,因此在以下部分中仅给出重要部分:

try:
        opts, args = getopt.getopt(sys.argv[1:], 'hle:t:p:cu:',
                                   ['help','listen','execute', 'target',
                                    'port', 'command', 'upload'])
    except getopt.GetoptError as err:
        print(str(err))
        usage()

    for o,a in opts:
        if o in ('-h', '--help'):
            usage()
        elif o in ('-l','--listen'):
            listen = True
        elif o in ('-e', '--execute'):
            execute = a
        elif o in ('-c','--commandshell'):
            command = True
        elif o in ('-u','--upload'):
            upload_destination = a
        elif o in ('-t','--target'):
            target = a
        elif o in ('-p','--port'):
            port = (int) a
        else:
            assert False,'Unhandled Option'

So, the a should be the port number which has string as type. 因此, a应该是具有字符串作为类型的端口号。 So, I thought that I should cast it. 所以,我认为我应该抛弃它。

The version of python is 2.7.3. python的版本是2.7.3。

I could not figure out why the error message appears. 我不知道为什么会出现错误消息。 I hope somebody can help. 希望有人能帮忙。

Best regards, 最好的祝福,

要从字符串a创建一个新的int对象,请使用

port = int(a)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM