Ambushed2
Ambushed2

Reputation: 13

Why is this regular expression not returning anything?

I've recently been working on a project in the field of machine learning, and I'm having some trouble with a regular expression that is needed for my data. For some reason, instead of returning a decimal value as it should, it ends up returning a value of None. Any suggestions? Below is my code, and the file I'm trying to get data out of.

for list_num in range(0,92):
    mach = [3,4,5,6,7]
    for j in range(0,4):
        if list_num >= 0 and list_num <= 4:
            if os.path.isfile('C://Users/avickers/Desktop/XFOIL_Training_Data/listofoutputs/listofoutputs0006'+str(mach[j])+'.pol'):
                tempaccess = os.listdir('C://Users/avickers/Desktop/XFOIL_Training_Data/listofoutputs')
                with open('C://Users/avickers/Desktop/XFOIL_Training_Data/listofoutputs/listofoutputs0006'+str(mach[j])+'.pol','r') as tempfile:
                    lines = tempfile.readlines()
                    file = tempfile.read()
                name = '0006'
                f = open('C://Users/avickers/Desktop/XFOIL_Training_Data/listofoutputs/data.pol','a+')
                Re =  re.search(r'Re\s*=\s*(\d\.\d+)',file)
                AOA = []
                Dl = []
                Dd = []
                num=0
                for line in lines[12:]:
                    columns = line.split()
                    AOA.append(columns[0])
                    Dl.append(columns[1])
                    Dd.append(columns[2])
                    num += 1
                for i in range(0,num):
                    f.write(name+' ')
                    f.write(str(mach[j]/10)+' ')
                    f.write(str(Re)+' ')
                    f.write(AOA[i]+' ')
                    f.write(Dl[i]+' ')
                    f.write(Dd[i]+'\n')

The file:

XFOIL         Version 6.99
Calculated polar for: NACA 0006
1 1 Reynolds number fixed          Mach number fixed         

xtrf =   1.000 (top)        1.000 (bottom)  
Mach =   0.300     Re =     2.213 e 6     Ncrit =   9.000

alpha    CL        CD       CDp       CM      Cpmin   XCpmin   Top_Xtr  
Bot_Xtr
------ -------- --------- --------- -------- -------- -------- -------- ----
-10.000  -0.7244   0.11348   0.11239   0.0360  -3.2095   0.0001   1.0000   
0.0032
-9.500  -0.7284   0.10263   0.10156   0.0311  -3.6224   0.0001   1.0000   
0.0033
-9.000  -0.7330   0.09190   0.09085   0.0257  -3.9746   0.0001   1.0000   
0.0036

The specific value I'm trying to extract is the Re. Thanks!

Upvotes: 1

Views: 68

Answers (1)

vks
vks

Reputation: 67968

The reason for None is file has nothing.

lines = tempfile.readlines()
file = tempfile.read()

Once lines is done with readlines(), the pointer tempfile is at the end.You cannot use it again to read.You need to open file again.

x="""XFOIL         Version 6.99
Calculated polar for: NACA 0006
1 1 Reynolds number fixed          Mach number fixed

xtrf =   1.000 (top)        1.000 (bottom)
Mach =   0.300     Re =     2.213 e 6     Ncrit =   9.000

alpha    CL        CD       CDp       CM      Cpmin   XCpmin   Top_Xtr
Bot_Xtr
------ -------- --------- --------- -------- -------- -------- -------- ----
-10.000  -0.7244   0.11348   0.11239   0.0360  -3.2095   0.0001   1.0000
 0.0032
-9.500  -0.7284   0.10263   0.10156   0.0311  -3.6224   0.0001   1.0000
0.0033
-9.000  -0.7330   0.09190   0.09085   0.0257  -3.9746   0.0001   1.0000
0.0036"""

import re
print re.search(r"Re\s*=\s*(\d\.\d+)", x).group(1)

Use group(1) . It prints 2.123

Upvotes: 2

Related Questions