0

Hi,

I try to read line by line from a file some data. Each line in that file has the following format:

x = 78.36734693877551, z = 11.428571428571429 -> amplitude: 8.62847057093655E-6 | phase -1.5707968246742405\n

I am trying to extract those four float numbers using regular expression. I came with the following code:

import re

myteststr='x = 78.36734693877551, z = 11.428571428571429 -> amplitude: 8.62847057093655E-6 | phase -1.5707968246742405\n'

rs=re.compile(r'^x = (\S+), z = (\S+) -> amplitude: (\S+) | phase (\S+)(\s*)$')
rss=rs.search(myteststr).groups()

The problem is that only the first 3 floats are extracted. The last one and the '\n' character aren't extracted - they are set to the None object:

>>> rss
('78.36734693877551', '11.428571428571429', '8.62847057093655E-6', None, None)

Please tell me where I do wrong. I want to be able to extract the last number also.

Best regards,

PTS

3
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by Gribouillis
1

I am not any special re optimizer, but wouldn't one use something similar to this?

import re
myteststr='x = 78.36734693877551, z = 11.428571428571429 -> amplitude: 8.62847057093655E-6 | phase -1.5707968246742405\n'
rs=re.compile('(-*\d+.\d+E*-*\d*)')
print map(float, rs.findall(myteststr))

Edited by pyTony: n/a

Votes + Comments
I learn something useful from this poster.
1

Hi,

I try to read line by line from a file some data. Each line in that file has the following format:

x = 78.36734693877551, z = 11.428571428571429 -> amplitude: 8.62847057093655E-6 | phase -1.5707968246742405\n

I am trying to extract those four float numbers using regular expression. I came with the following code:

import re

myteststr='x = 78.36734693877551, z = 11.428571428571429 -> amplitude: 8.62847057093655E-6 | phase -1.5707968246742405\n'

rs=re.compile(r'^x = (\S+), z = (\S+) -> amplitude: (\S+) | phase (\S+)(\s*)$')
rss=rs.search(myteststr).groups()

The problem is that only the first 3 floats are extracted. The last one and the '\n' character aren't extracted - they are set to the None object:

>>> rss
('78.36734693877551', '11.428571428571429', '8.62847057093655E-6', None, None)

Please tell me where I do wrong. I want to be able to extract the last number also.

Best regards,

PTS

The vertical bar is a special character in re. You can use

rs=re.compile(r'^x = (\S+), z = (\S+) -> amplitude: (\S+) \| phase (\S+)(?:\s*)$')

Also this looks nice http://stackoverflow.com/questions/385558/python-and-regex-question-extract-float-double-value

Edited by Gribouillis: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.