python - 从字符串中,如何在 rgb 之后的括号之间提取值?
问题描述
这是我上一篇文章的分支Python get info from long complex string
该字符串来自我正在解析的一个奇怪的 XML 文件
我想知道如何使用 re 从这个字符串中获取 rgb 值
style="fill: rgb(100, 2, 5); fill-opacity: 1; font-family: ProjectStocksFont; font-size: 70px; font-weight: normal; font-style: normal; text-decoration: none;"
- 以后要使用它们,例如:
print(rgb[0], rgb[1], rgb[2])
# 100 2 5
解决方案
我会将字符串从左括号拼接到右括号,然后使其成为一个列表。就像是:
rgb = style[style.find("(") + 1:style.find(")")].split(", ")
rgb 将是一个列表: ['100' , '2' , '5']
推荐阅读
- java - 用于嵌套 for 循环的 Lambda
- sql - 在给定 IDS 列表的情况下,对表字段中的多个值利用 postgres ILIKE 函数的 SQL 查询
- c++ - 查找用于提升多精度 uint512_t 的 First Set 指令 (ffs)
- typescript - 我如何禁用/处理初始化,toJSON
- uiscrollview - 为什么滚动时 UIScrollView 会颠簸
- kotlin - 我需要手动停止处理程序吗?
- java - 选择时如何将颜色箭头图标更改为 JMenu
- javascript - nodejs中连接模块的顺序是什么?
- c++ - 信号:分段错误 (11) 信号代码:无效权限 (2)
- c - 用于打印数组中元素位置的C函数不起作用