解析一行文本以获取一个特定的数字 [英] parsing a line of text to get a specific number
本文介绍了解析一行文本以获取一个特定的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一行文字,格式为" some spaces variable = 7 = '0x07' some more data"
I have a line of text in the form " some spaces variable = 7 = '0x07' some more data"
我想解析它并从某些变量= 7"中获得数字7.如何在python中完成?
I want to parse it and get the number 7 from "some variable = 7". How can this be done in python?
推荐答案
我将使用更简单的解决方案,避免使用正则表达式.
I would use a simpler solution, avoiding regular expressions.
在'='上分割并在期望的位置获取值
Split on '=' and get the value at the position you expect
text = 'some spaces variable = 7 = ...'
if '=' in text:
chunks = text.split('=')
assignedval = chunks[1]#second value, 7
print 'assigned value is', assignedval
else:
print 'no assignment in line'
这篇关于解析一行文本以获取一个特定的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文