【问题标题】:Extracting a scientific notation number using regexp in python在python中使用正则表达式提取科学计数法数字
【发布时间】:2013-10-24 18:27:34
【问题描述】:

我正在尝试从 python 中的大量文本文件中提取值。我需要的数字是科学记数法形式。我的结果文本文件如下

    ADDITIONAL DATA

Tip Rotation (degrees)
Node    , UR[x] , UR[y] , UR[z]
21  , 1.0744    , 1.2389    , -4.3271
22  , -1.0744   , -1.2389   , -4.3271
53  , 0.9670    , 1.0307    , -3.8990
54  , -0.0000   , -0.0000   , -3.5232
55  , -0.9670   , -1.0307   , -3.8990

Mean rotation variation along blade
Region  , Rotation (degrees)
Partition line 0,   7.499739E-36
Partition line 1,   -3.430092E-01
Partition line 2,   -1.019287E+00
Partition line 3,   -1.499808E+00
Partition line 4,   -1.817651E+00
Partition line 5,   -2.136372E+00
Partition line 6,   -2.448321E+00
Partition line 7,   -2.674414E+00
Partition line 8,   -2.956737E+00
Partition line 9,   -3.457806E+00
Partition line 10,  -3.995106E+00

我过去一直在成功使用正则表达式,但它似乎不想获取数字。我的结果文件中的节点数发生了变化,因此无法按行搜索。我的python脚本如下。

import re
from pylab import *
from scipy import *
import matplotlib
from numpy import *
import numpy as np
from matplotlib import pyplot as plt
import csv

########################################

minTheta        = -90
maxTheta        = 0
thetaIncrements = 10

numberOfPartitions = 10

########################################

numberOfThetas = ((maxTheta - minTheta)/thetaIncrements)+1
print 'Number of thetas = '+str(numberOfThetas)

thetas              = linspace(minTheta,maxTheta,numberOfThetas)
print 'Thetas = '+str(thetas)
part                = linspace(1,numberOfPartitions,numberOfPartitions)
print 'Parts = '+str(part)

meanRotations       = np.zeros((numberOfPartitions+1,numberOfThetas))
#print meanRotations

theta = minTheta
n=0
m=0
while theta <= maxTheta:

        fileName = str(theta)+'.0.txt'
        #print fileName

        regexp = re.compile(r'Partition line 0, .*?([-+0-9.E]+)')
        with open(fileName) as f:
            for line in f:
                match = regexp.match(line)
                if match:
                    print (float((match.group(1))))
                    meanRotations[0,m]=(float((match.group(1))))

        regexp = re.compile(r'Partition line 1, .*?([-+0-9.E]+)')
        with open(fileName) as f:
            for line in f:
                match = regexp.match(line)
                if match:
                    print (float((match.group(1))))
                    meanRotations[1,m]=(float((match.group(1))))

        regexp = re.compile(r'Partition line 2, .*?([-+0-9.E]+)')
        with open(fileName) as f:
            for line in f:
                match = regexp.match(line)
                if match:
                    print (float((match.group(1))))
                    meanRotations[2,m]=(float((match.group(1))))

        regexp = re.compile(r'Partition line 3, .*?([-+0-9.E]+)')
        with open(fileName) as f:
            for line in f:
                match = regexp.match(line)
                if match:
                    print (float((match.group(1))))
                    meanRotations[3,m]=(float((match.group(1))))

        regexp = re.compile(r'Partition line 4, .*?([-+0-9.E]+)')
        with open(fileName) as f:
            for line in f:
                match = regexp.match(line)
                if match:
                    print (float((match.group(1))))
                    meanRotations[4,m]=(float((match.group(1))))

        regexp = re.compile(r'Partition line 5, .*?([-+0-9.E]+)')
        with open(fileName) as f:
            for line in f:
                match = regexp.match(line)
                if match:
                    print (float((match.group(1))))
                    meanRotations[5,m]=(float((match.group(1))))

        regexp = re.compile(r'Partition line 6, .*?([-+0-9.E]+)')
        with open(fileName) as f:
            for line in f:
                match = regexp.match(line)
                if match:
                    print (float((match.group(1))))
                    meanRotations[6,m]=(float((match.group(1))))

        regexp = re.compile(r'Partition line 7, .*?([-+0-9.E]+)')
        with open(fileName) as f:
            for line in f:
                match = regexp.match(line)
                if match:
                    print (float((match.group(1))))
                    meanRotations[7,m]=(float((match.group(1))))

        regexp = re.compile(r'Partition line 8, .*?([-+0-9.E]+)')
        with open(fileName) as f:
            for line in f:
                match = regexp.match(line)
                if match:
                    print (float((match.group(1))))
                    meanRotations[8,m]=(float((match.group(1))))

        regexp = re.compile(r'Partition line 9, .*?([-+0-9.E]+)')
        with open(fileName) as f:
            for line in f:
                match = regexp.match(line)
                if match:
                    print (float((match.group(1))))
                    meanRotations[9,m]=(float((match.group(1))))

        regexp = re.compile(r'Partition line 10, .*?([-+0-9.E]+)')
        with open(fileName) as f:
            for line in f:
                match = regexp.match(line)
                if match:
                    print (float((match.group(1))))
                    meanRotations[10,m]=(float((match.group(1))))

        m=m+1
        theta = theta+thetaIncrements

print 'Mean rotations on partition lines = '
print meanRotations

任何帮助将不胜感激!

【问题讨论】:

  • 为什么不去掉标题('Partition line x')而只使用 re.findall() 代替?
  • 这可以通过循环来简化。相同的代码写 10 次是没有意义的。
  • 打开并迭代文件n次也没有意义。
  • 另外,检查您的导入。不推荐这种导入from pylab import *。而且您要导入两次相同的模块。
  • Diego,一开始我确实使用了一个循环,但当它不起作用时,我决定确保这不是问题,所以把它完整地写下来。 Corley,结果文件中的数据比我显示的要多,这会影响你的提议吗?

标签: python qregexp


【解决方案1】:

这种文件格式是标准的吗?如果是这样?您可以使用另一种技术获得所有浮点值。 所以,这里是代码:

str = """    ADDITIONAL DATA

Tip Rotation (degrees)
Node    , UR[x] , UR[y] , UR[z]
21  , 1.0744    , 1.2389    , -4.3271
22  , -1.0744   , -1.2389   , -4.3271
53  , 0.9670    , 1.0307    , -3.8990
54  , -0.0000   , -0.0000   , -3.5232
55  , -0.9670   , -1.0307   , -3.8990

Mean rotation variation along blade
Region  , Rotation (degrees)
Partition line 0,   7.499739E-36
Partition line 1,   -3.430092E-01
Partition line 2,   -1.019287E+00
Partition line 3,   -1.499808E+00
Partition line 4,   -1.817651E+00
Partition line 5,   -2.136372E+00
Partition line 6,   -2.448321E+00
Partition line 7,   -2.674414E+00
Partition line 8,   -2.956737E+00
Partition line 9,   -3.457806E+00
Partition line 10,  -3.995106E+00
"""

arr = str.split()
for index in enumerate(arr):
    print index  # just to see the list
start = 59  # from this position the numbers begin
step = 4    # current number is each fourth
ar = []
for j in range(start, len(arr), step):
    ar.append(arr[j])

floatAr = []
# or you can use this expression instead of the following loop 
# floatAr = [float(x) for x in ar]
for n in range(len(ar)):
    floatAr.append(float(ar[n]))

print floatAr

最后,您将收到一个名为 floatAr 的列表,其中包含所有浮点值。您可以添加 try-except 块以获得更好的可用性。

或者,如果您想使用正则表达式,代码如下:

<!--language:python -->

str = """   ADDITIONAL DATA

Tip Rotation (degrees)
Node    , UR[x] , UR[y] , UR[z]
21  , 1.0744    , 1.2389    , -4.3271
22  , -1.0744   , -1.2389   , -4.3271
53  , 0.9670    , 1.0307    , -3.8990
54  , -0.0000   , -0.0000   , -3.5232
55  , -0.9670   , -1.0307   , -3.8990

Mean rotation variation along blade
Region  , Rotation (degrees)
Partition line 0,   7.499739E-36
Partition line 1,   -3.430092E-01
Partition line 2,   -1.019287E+00
Partition line 3,   -1.499808E+00
Partition line 4,   -1.817651E+00
Partition line 5,   -2.136372E+00
Partition line 6,   -2.448321E+00
Partition line 7,   -2.674414E+00
Partition line 8,   -2.956737E+00
Partition line 9,   -3.457806E+00
Partition line 10,  -3.995106E+00"""

regex = '\s-?[1-9]+[0-9]*.?[0-9]*E-?\+?[0-9]+\s?'

import re

values = re.findall(regex, str)
floatAr = [float(x) for x in values]
print floatAr

顺便说一下,这里有一个不错的python 在线正则表达式检查器pythex

【讨论】:

    【解决方案2】:

    说实话,我不需要正则表达式。像这样的东西应该可以满足您的需求:

    with open(fileName) as f:
        for line in f:
            if line.startswith('Partition line'):
                number=float(line.split(',')[1])
                print number # or do whatever you want with it
            # read other file contents with different if clauses
    

    【讨论】:

    • 成功了,谢谢。但是我怎么能把这些值放在矩阵形式中,即每个文件都有自己的行,每个分区行都有自己的列(反之亦然)。所以我会看一个 n*11 的矩阵,其中 n=文件数?
    • 要么使用您选择的矩阵/数组类型(例如来自numpy),然后依次填充它,要么您制作一个列表列表,并为您加载的每个文件附加列出该对象的完整分区行条目。不过,后者有点棘手,请参阅stackoverflow.com/questions/11487049/python-list-of-lists/…
    • 感谢您的帮助,但想到了另一种方法,制作一个大列表,然后将其重新调整大小!知道如何从上述值中取出一行值以产生行差异,然后生成一个少 1 行的矩阵吗?
    • 我不是 100% 确定你在问什么,但 Numpy.diff 似乎是你想要的:docs.scipy.org/doc/numpy/reference/generated/numpy.diff.html
    猜你喜欢
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    • 2018-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多