Cast将arg传递给string

Phi*_*ord 1 python arguments casting string-formatting

Python的新手,也许我没有说明问题,但是如何在python中将传递的arg转换为字符串?

这是我正在尝试的:

#!/usr/bin/python
# Python Wrapper to Call XMLRPC service

import xmlrpclib
import sys  

# Set the Server
servAddr = "http://127.0.0.1/xmlrpc.server.php"

# Start the Client
client = xmlrpclib.ServerProxy(servAddr)

for arg in sys.argv:
    id = str(arg)
    print client.service.setId(id) # Throws long error
    # print client.service.setId('123') # Hard coded works
    #print arg # prints the id passed
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 9

sys.argv已经是字符串的值.我认为问题是你传递的是sys.argv [0],这是脚本名称.试试这个:

for arg in sys.argv[1:]:
    print client.service.setId(arg)
Run Code Online (Sandbox Code Playgroud)