I want to print all names with the example below. Names are random every time.
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\"]"
I know how to print first of this names:
print str(txt[txt.index('[something name=\"')+17:txt.index(' other')-1])
but how can I print all? I need to print all names in new line:
Paul
James
Victor
Jane
Looks like you can use regex here:
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
Prints:
Paul
James
Victor
Jane
Another approach would be to split the string as follows:
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 x in txt.split(']'):
if len(x):
print x.split('"', 2)[1]
Giving:
Paul
James
Victor
Jane
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.