2024-09-30 18:16:19 发布
网友
我试图从我解析的字符串中获取信息。我想知道字体大小。这是我返回的字符串
style="fill: rgb(0, 0, 0); fill-opacity: 1; font-family: ProjectStocksFont; font-size: 70px; font-weight: normal; font-style: normal; text-decoration: none;"
我想从font-size那里得到70最好的方法是什么
font-size
70
使用.split()方法是一种方法
.split()
可以将字符串拆分为python列表,以分隔字符串中的每种条目。然后,您可以迭代列表,拆分每个子字符串,并将值保存在python字典中
style = "fill: rgb(0, 0, 0); fill-opacity: 1; font-family: ProjectStocksFont; font-size: 70px; font-weight: normal; font-style: normal; text-decoration: none;" style_dict = {} style = style.split("; ") for item in style: key, value = item.split(": ") style_dict[key] = value key = "font-size" print(style_dict[key])
您可以为任务使用re模块:
re
style="fill: rgb(0, 0, 0); fill-opacity: 1; font-family: ProjectStocksFont; font-size: 70px; font-weight: normal; font-style: normal; text-decoration: none;" import re print( re.search(r'font-size:\s*(\d+)', style)[1] )
印刷品:
或者,您可以将字符串解析为字典。如果您希望访问的不仅仅是该属性,那么这可能很有用
style_dict = dict([[a.strip() for a in s.split(':')] for s in style.split(';') if s != ""]) style_dict['font-size']
给予
'70px'
如果您不想要这些装置:
style_dict['font-size'][:-2]
使用
.split()
方法是一种方法可以将字符串拆分为python列表,以分隔字符串中的每种条目。然后,您可以迭代列表,拆分每个子字符串,并将值保存在python字典中
您可以为任务使用
re
模块:印刷品:
或者,您可以将字符串解析为字典。如果您希望访问的不仅仅是该属性,那么这可能很有用
给予
如果您不想要这些装置:
相关问题 更多 >
编程相关推荐