Python:将列表项替换为字符串的有效方法

发布于 2024-11-04 19:33:08 字数 751 浏览 0 评论 0原文

我正在为 Minecraft 服务器创建一个包装器,该包装器应该从名为命令的目录中获取新命令,该目录包含以命令命名的文件,所有文件都包含用于组成该命令的服务器命令。例如,以下代码片段来自定义“tell”命令的文件:

tell <1> <sender> says: <2>

在内部,包装器读取服务器进程的标准输出,查找用户正在运行命令的指示。然后它会分割命令,从中获取名称“sender”(显然是发送命令的人)、“command”(指示命令的单字字符串)和一个名为 args 的列表,其中包含参数跟随命令字符串。例如,tell 命令的语法是这样的:

tell jim hello

产生以下名称:

sender = s0lder
command = tell
args = ['jim', 'hello']

我的问题是,假设上面的例子,我怎样才能制作最终的字符串,说“输出”,读:

tell jim s0lder says: hello

我基本上需要一种方法,将定义字符串中括号包围的区域替换为 args 列表中相应的名称/项目,以便:

<sender> = sender
<1> = args[0]
<2> = args[1]

对于 args 列表中的所有项目,依此类推。谢谢。

I'm creating a wrapper for a Minecraft Server that is supposed to take new commands from a directory called commands, which contains files named after the command, all containing the server commands used to make up that command. For example, the following snippet is from the file that defines the "tell" command:

tell <1> <sender> says: <2>

Internally, the wrapper reads the stdout of the server process, looking for indications that a user is running a command. It then splits the command up, taking from it the name "sender" which is, obviously, the person who sent the command, "command" which is a one word string indicating the command, and a list called args, which contains the arguments following the command string. For example, the syntax of the tell command is this:

tell jim hello

Which results in the following names:

sender = s0lder
command = tell
args = ['jim', 'hello']

My question is, assuming the above examples, how can I make the final string, say "output", read:

tell jim s0lder says: hello

I need a way basically, to substitute the areas surrounded by brackets in the definition string, with the corresponding names/items of the args list so that:

<sender> = sender
<1> = args[0]
<2> = args[1]

and so on for all of the items in the args list. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

灼疼热情 2024-11-11 19:33:08

这是一个不需要您更改格式的解决方案(这一切都是以编程方式完成的)。

sender = 's0lder'
args = ['jim', 'hello']

format = "tell <1> <sender> says: <2>".replace("<", "%(<").replace(">", ">)s")
# format == 'tell %(<1>)s %(<sender>)s says: %(<2>)s'
subs = {"<sender>": sender, "<1>": args[0], "<2>": args[1]}
print format % subs
# 'tell jim s0lder says: hello'

Here is a solution that doesn't require you to change your format (it is all done programatically).

sender = 's0lder'
args = ['jim', 'hello']

format = "tell <1> <sender> says: <2>".replace("<", "%(<").replace(">", ">)s")
# format == 'tell %(<1>)s %(<sender>)s says: %(<2>)s'
subs = {"<sender>": sender, "<1>": args[0], "<2>": args[1]}
print format % subs
# 'tell jim s0lder says: hello'
愚人国度 2024-11-11 19:33:08

如果您可以忍受稍微更改格式字符串,那么内置的 format 函数应该足够了

args = ["jim", "hello"]
kwargs = {"sender": "s0lder"}
print("tell {0} {sender} says: {1}".format(*args, **kwargs))

tell jim s0lder says: hello

If you can live with changing the format strings a little bit, the built-in format function should be quite sufficient:

args = ["jim", "hello"]
kwargs = {"sender": "s0lder"}
print("tell {0} {sender} says: {1}".format(*args, **kwargs))

outputs

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