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

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))
commented: I learn something useful from this poster. +3

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.