简体   繁体   English

如何使用Python解析格式化的字符串

[英]How to parse a formatted string using Python(re)

The string I want to parse is like "{average:12.1km/ltr}". 我要解析的字符串就像“ {average:12.1km / ltr}”。 I want to extract 12.1 from this string. 我想从该字符串中提取12.1。 The only way I know is using split(":") and split("km/ltr") or so, but these seem not useful. 我知道的唯一方法是使用split(“:”)和split(“ km / ltr”)左右,但是这些似乎没有用。 I want to use the scanf-like method to extract 12.1, but in python document, it shows that using regular expression is better than scanf-like function. 我想使用类似scanf的方法来提取12.1,但在python文档中,它表明使用正则表达式要优于类似于scanf的函数。 I though regular express cannot be used in extraction. 我虽然不能使用正则表达式提取。 How should I extract this using re? 我应该如何使用re提取它?

I think you could have simply used the following to extract the numeric portion from the string. 我认为您可以简单地使用以下内容从字符串中提取数字部分。

  • The Trick is, there is one and only one number with a period between. 窍门是,只有一个数字,并且中间有一个句点。
  • Period may be optional, as you number may be a whole integer 句点可能是可选的,因为您的数字可能是整数
  • You may also encounter fractional numbers 您可能还会遇到小数

Here is the sample 这是样本

>>> re.findall("\d+\.?\d*|\.\d+",st)
>>> st = "{average:12.1km/ltr}"
>>> re.findall("\d+\.?\d*|\.\d+",st)
['12.1']
>>> st = "{average:12km/ltr}"
>>> re.findall("\d+\.?\d*|\.\d+",st)
['12']
>>> st = "{average:.5km/ltr}"
>>> re.findall("\d+\.?\d*|\.\d+",st)
['.5']
>>> st = "{average:12.km/ltr}"
>>> re.findall("\d+\.?\d*|\.\d+",st)
['12.']
>>> st = " {max:26.9kgm@6100rpm}"
>>> re.findall("\d+\.?\d*|\.\d+",st)
['26.9', '6100']

Just strip all characters you don't want - no need for regular expressions (though I like them...) 只需strip所有不需要的字符-不需要正则表达式(尽管我喜欢它们...)

>>> import string
>>> s = "{average:12.1km/ltr}"
>>> s2 = s.strip(string.ascii_letters + "{}:/")
>>> print s2
12.1
>>> number = float(s2)
>>> print number
12.1

Try this, assuming the number could be without dot. 假设数字可能不带点,请尝试此操作。

import re
re.findall('[0-9]+(\.[0-9]+)?', str)

how about dirty and quick 怎么又脏又快

re.findall('[\d.]+',s)

this works for your example. 这适用于您的示例。

You said you tried to split(":") and split("km/ltr") , so I'll suppose that the format of the string is always like :__X__km/ltr , where __X__ is a number. 您说您尝试过split(":")split("km/ltr") ,所以我假设字符串的格式总是像:__X__km/ltr ,其中__X__是一个数字。

The following regex will work: 以下正则表达式将起作用:

:(\d.+)km

Example: 例:

>>> import re
>>> re.findall(':(\d.+)km', '{average:12.1km/ltr}')
['12.1']
>>>

Then you can just parse as float using the float() function. 然后,您可以使用float()函数将其解析为float。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM