Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中使用字符串:以非常特定的方式格式化信息_Python_String_Python 3.x - Fatal编程技术网

在Python中使用字符串:以非常特定的方式格式化信息

在Python中使用字符串:以非常特定的方式格式化信息,python,string,python-3.x,Python,String,Python 3.x,OBS:我正在使用Python3.5.2,我使用标准的Shell和IDLE 大家好,我认为使用python编程时最困难的任务之一是处理字符串。有人能帮忙吗 我经常遇到的问题之一是转换字符串,以便正确使用它。例如: #consider I have the following string I want to work with: my_input='插入3[“psyduck”,30]!!!插入10[“海滩上美好的一天”,100]!!!找到3个!!!查找10' 我如何转换此字符串,使其能够根据结

OBS:我正在使用Python3.5.2,我使用标准的Shell和IDLE

大家好,我认为使用python编程时最困难的任务之一是处理字符串。有人能帮忙吗

我经常遇到的问题之一是转换字符串,以便正确使用它。例如:

#consider I have the following string I want to work with:
my_input='插入3[“psyduck”,30]!!!插入10[“海滩上美好的一天”,100]!!!找到3个!!!查找10'

我如何转换此字符串,使其能够根据结果执行以下操作:

for entry in listOfCommands:
    if entry[0] == 'insert':
    #I will execute a part of the program    

    elif entry[0] == 'update':
    #execute something else

    elif entry[0] == 'find':
    #execute something else
1-将以下子字符串分成如下变量:

2-或任何其他最终能让我做到这一点的解决方案:

我需要此列表以便执行以下操作:

for entry in listOfCommands:
    if entry[0] == 'insert':
    #I will execute a part of the program    

    elif entry[0] == 'update':
    #execute something else

    elif entry[0] == 'find':
    #execute something else
问题是我不知道输入中会出现什么(命令的数量或我必须添加的信息的大小)。我只知道它将始终遵循以下确切格式: [我必须在其中存储或更新信息的命令、节点、我必须存储或更新的信息][我必须查找或删除的命令、节点],块之间用“!!”隔开


我可以用自己的方式处理主程序,但为了使其正常运行,我需要以这种非常特殊的方式格式化此输入。

也许类似的方式可以:

commands = my_input.split('!!!')
my_commands = [c.split(' ', 2) for c in commands]

split
方法的第二个参数告诉它您希望它拆分字符串的次数。

是的,根据输入的复杂性,这个小技巧可能会奏效。要获得列表,您可以始终
ast.literal\u eval
。是的,我没有分析第三个参数,因为它是相应命令的工作,但很可能
json
ast.literal_eval
都可以工作。
commands = my_input.split('!!!')
my_commands = [c.split(' ', 2) for c in commands]