Python:将列表项替换为字符串的有效方法
我正在为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个不需要您更改格式的解决方案(这一切都是以编程方式完成的)。
Here is a solution that doesn't require you to change your format (it is all done programatically).
如果您可以忍受稍微更改格式字符串,那么内置的
format
函数应该足够了:
If you can live with changing the format strings a little bit, the built-in
format
function should be quite sufficient:outputs