将传递的 arg 转换为字符串

发布于 2024-10-10 07:56:17 字数 512 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

煞人兵器 2024-10-17 07:56:18

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

for arg in sys.argv[1:]:
    print client.service.setId(arg)

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:

for arg in sys.argv[1:]:
    print client.service.setId(arg)
红墙和绿瓦 2024-10-17 07:56:18

sys.argv 中的参数应该已经是字符串。是什么让您认为需要将其转换为字符串?

您不认为您应该发布该错误吗?

但无论如何,你都做错了。 argv 的第一个参数是脚本的名称。您可能还想

for arg in sys.argv[1:]:
    id = str(arg)
    print client.service.setId(id)

,您应该在循环中放置一条 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

for arg in sys.argv[1:]:
    id = str(arg)
    print client.service.setId(id)

Also, you should have just put a print statement in the loop to see exactly what was being passed to setId().

清风夜微凉 2024-10-17 07:56:18

看起来你想要一个整数。传入的每个 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. Try int(arg) instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文