gpt4 book ai didi

Python 2.7 打印字符串

转载 作者:太空宇宙 更新时间:2023-11-04 08:05:00 32 4
gpt4 key购买 nike

我想用下面的例子打印所有的名字。名称每次都是随机的。

txt = "[something name=\"Paul\" other=\"1/1/1\"][something name=\"James\" other=\"4/3/5\"][something name=\"Victor\" other=\"7/2/6\"][something name=\"Jane\" other=\"4/3/6\"]"

我知道如何打印第一个名字:

print str(txt[txt.index('[something name=\"')+17:txt.index(' other')-1])

但是我怎样才能全部打印出来呢?我需要在新行中打印所有名称:

Paul
James
Victor
Jane

最佳答案

看起来你可以在这里使用正则表达式:

import re
txt = "[something name=\"Paul\" other=\"1/1/1\"][something name=\"James\" other=\"4/3/5\"][something name=\"Victor\" other=\"7/2/6\"][something name=\"Jane\" other=\"4/3/6\"]"
for name in re.findall('name\=\\"(.*?)\\\"', txt):
print name

打印:

Paul
James
Victor
Jane

关于Python 2.7 打印字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32761438/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com