简体   繁体   English

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

[英]Python: Efficient Way to Substitute an Item of a List into a String

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.我正在为 Minecraft 服务器创建一个包装器,它应该从名为 commands 的目录中获取新命令,该目录包含以命令命名的文件,所有文件都包含用于构成该命令的服务器命令。 For example, the following snippet is from the file that defines the "tell" command:例如,以下片段来自定义“tell”命令的文件:

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.然后它将命令拆分,取名为“sender”,显然是发送命令的人,“command”是一个指示命令的单字字符串,以及一个名为 args 的列表,其中包含 arguments在命令字符串之后。 For example, the syntax of the tell command is this:例如,tell 命令的语法是这样的:

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:我基本上需要一种方法,用 args 列表的相应名称/项目替换定义字符串中括号包围的区域,以便:

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

and so on for all of the items in the args list.对于 args 列表中的所有项目,依此类推。 Thanks.谢谢。

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'

If you can live with changing the format strings a little bit, the built-in format function should be quite sufficient:如果您可以稍微更改格式字符串,那么内置format function 应该足够了:

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

outputs输出

tell jim s0lder says: hello

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python比较元组列表中项目的有效方法 - python efficient way to compare item in list of tuples python - 检查部分字符串是否在列表中的有效方法 - python - efficient way of checking if part of string is in the list 在 Python 的列表中查找第二大项目的更有效方法 - More Efficient Way to find the Second Largest Item in a List in Python 在python中替换重复np.vstack的有效方法? - Efficient way to substitute repeating np.vstack in python? 在 Python 中仅替换正则表达式字符串的一部分的方法 - Way to substitute only part of a regex string in Python 在Python中以字符串格式对日期列表进行排序的最有效方法是什么? - What is the most efficient way to sort a list of dates in string format in Python? Python:分解字符串并将其附加到列表的更有效方法 - Python: More efficient way to break down string and append it to a list Python将字符串解析为文件浮点列表的有效方法 - Python Efficient way of parsing string to list of floats from a file Python 在列表中选择最长字符串的最有效方法? - Python's most efficient way to choose longest string in list? 在 python 的单个元素列表中检查字符串是否存在的有效方法是什么 - What is the efficient way to check a present of a string in a single element list in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM