将传递的 arg 转换为字符串
Python 新手,也许我没有正确地提出问题,但是如何将传递的 arg 转换为 python 中的字符串?
这是我正在尝试的:
#!/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
Newbie at Python and maybe I'm not stating the question right, but how do I cast a passed arg to string in python?
Here is what I'm trying:
#!/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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
sys.argv
中的值已经是字符串。我认为问题是您正在传递 sys.argv[0] 这是脚本名称。试试这个:The values from
sys.argv
already are strings. I think the problem is that you are passing sys.argv[0] which is the script name. Try this:sys.argv 中的参数应该已经是字符串。是什么让您认为需要将其转换为字符串?
您不认为您应该发布该错误吗?
但无论如何,你都做错了。 argv 的第一个参数是脚本的名称。您可能还想
,您应该在循环中放置一条 print 语句,以准确查看传递给 setId() 的内容。
Your args in sys.argv should already be strings. What makes you think you need to cast it o a string?
Don't you think you should probably post the error?
But anyway, you're doing it wrong anyway. The first argument to argv is the name of the script. You probably want
Also, you should have just put a print statement in the loop to see exactly what was being passed to setId().
看起来你想要一个整数。传入的每个
arg
已经是一个字符串,因此没有理由像您所做的那样对其进行强制转换。请尝试使用int(arg)
来代替。It looks as though you want an integer. Each
arg
that gets passed in is already a string so there would be no reason to cast it as you are doing. Tryint(arg)
instead.