我尝试从一个字符串中获取一个特定的子字符串,其频率与Python中的相同。 子字符串每次都不同,但结构相同
例如:
[{u'style': u'opacity:0.58800001;fill:#0000ff;fill-opacity:1;
stroke:none;stroke-width:3.47952747;
stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;
stroke-dashoffset:0;stroke-opacity:1', u'id': u'rect5231', u'd':
u'm 0,1016.9291 35.433071,0 0,35.433 -35.433071,0 z
'},{u'style': u'opacity:0.58800001;fill:#0000ff;fill-opacity:1;
stroke:none;stroke-width:3.47952747;stroke-linecap:round;
stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;
stroke-dashoffset:0;stroke-opacity:1', u'id': u'rect5233', u'd':
u'm 70.866142,1016.9291 35.433068,0 0,35.433 -35.433068,0 z
'}]
这就是我拥有的字符串,我只需要以“m…”开头的单独子字符串 我尝试了str.split,但它只删除了第二个标记的子字符串
拥有一个字符串,您可以在
while loop
中使用find()
方法。从字符串的开头开始,使用find()
方法查找第一个匹配项,该方法返回子字符串开头的索引,然后再次开始,但这次从前面的find()
找到的后面的索引开始。依此类推,除非find()
返回-1
为了根据字符串中的某种模式找到子字符串,可以对正则表达式使用
re
模块输出
正如您所见,检索所需的值很容易,它是一个
str
相关问题 更多 >
编程相关推荐