我试图解析SVG文件的一部分,以获得十六进制填充颜色(比如#ceff80等)
当我使用Bashgrep
命令执行此操作时:
grep -o -e "fill:#[a-f, 0-9]*;" Sample.svg
结果如下:
^{pr2}$但是当我用Python的re
模块尝试时,我得到了None
:
import re
color = re.match(r'fill:#[a-f, 0-9]*;', style)
这是在使用ElementTree
遍历XML结构的循环中调用的。style
字符串包含如下值:
font-style:normal;font-weight:normal;font-size:40px;line-height:125%;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;display:inline;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;filter:url(#filter5107)
我想提取fill:#xxxxxx;color,然后用[6:-1]
范围表达式截断它,以便进一步处理。在
由于某些原因,它总是返回None
类型的对象。在
我做错什么了?在
您应该使用
re.search()
而不是re.match()
。在re.match()
只检查字符串是否与RegExp完全匹配,因此如果开头有任何意外情况,它将失败。在相关问题 更多 >
编程相关推荐