python正则表达式错误:NameError:name' re'没有定义

时间:2016-07-20 17:48:46

标签: python regex

我有一些代码可以读取infofile并使用python的正则表达式提取信息并将其写入新文件。当我在自己的脚本中单独测试这部分代码时,它可以完美地工作。但是,当我将其添加到我的其余代码时,我收到此错误:

NameError: name 're' is not defined

以下是我的整个代码。正则表达式部分是显而易见的(所有re.search命令):

import glob
import subprocess
import os
import datetime
import matplotlib.pyplot as plt 
import csv
import re
import ntpath

x = open('data.txt', 'w')
m = open('graphing_data.txt', 'w')

ckopuspath= '/Volumes/DAVIS/sfit-ckopus/ckopus'
command_init = 'sfit4Layer0.py -bv5 -fh'

subprocess.call(command_init.split(), shell=False)


with open('/Volumes/DAVIS/calpy_em27_neu/spectra_out_demo/info.txt', 'rt') as infofile: # the info.txt file created by CALPY
    for count, line in enumerate(infofile): 
        with open('\\_spec_final.t15', 'w') as t:
            lat = re.search('Latitude of location:\s*([^;]+)', line, re.IGNORECASE).group(0)
            lat = lat.split()
            lat = lat[3]
            lat = float(lat)

            lon = re.search('Longitude of location:\s*([^;]+)', line, re.IGNORECASE).group(0)
            lon = lon.split()
            lon = lon[3]
            lon = float(lon)

            date = re.search('Time of measurement \(UTC\): ([^;]+)', line).group(0) 
            date = date.split()

            yeardate = date[4]
            yeardate = yeardate.split('-')
            year = int(yeardate[0])
            month = int(yeardate[1])
            day = int(yeardate[2])

            time = date[5]    
            time = time.split(':')
            hour = int(time[0])
            minute = int(time[1])
            second = float(time[2])    

            dur = re.search('Duration of measurement \[s\]: ([^;]+)', line).group(0)
            dur = dur.split()
            dur = float(dur[4])

            numpoints = re.search('Number of values of one scan:\s*([^;]+)', line, re.IGNORECASE).group(0)
            numpoints = numpoints.split()
            numpoints = float(numpoints[6])

            fov = re.search('semi FOV \[rad\] :\s*([^;]+)', line, re.IGNORECASE).group(0)
            fov = fov.split()
            fov = fov[3]
            fov = float(fov[1:])

            sza = re.search('sun Azimuth \[deg\]:\s*([^;]+)', line, re.IGNORECASE).group(0)
            sza = sza.split()
            sza = float(sza[3])

            snr = 0.0000
            roe = 6396.2
            res = 0.5000

            lowwav = re.search('first wavenumber:\s*([^;]+)', line, re.IGNORECASE).group(0)
            lowwav = lowwav.split()
            lowwav = float(lowwav[2])

            highwav = re.search('last wavenumber:\s*([^;]+)', line, re.IGNORECASE).group(0)
            highwav = highwav.split()
            highwav = float(highwav[2])

            spacebw = (highwav - lowwav)/ numpoints


            d = datetime.datetime(year, month, day, hour, minute, second)
            t.write('{:>12.5f}{:>12.5f}{:>12.5f}{:>12.5f}{:>8.1f}'.format(sza,roe,lat,lon,snr)) # line 1
            t.write("\n")
            t.write('{:>10d}{:>5d}{:>5d}{:>5d}{:>5d}{:>5d}'.format(year,month,day,hour,minute,second)) # line 2
            t.write("\n")
            t.write( ('{:%Y/%m/%d %H:%M:%S}'.format(d)) + "UT Solar Azimuth:" + ('{:>6.3f}'.format(sza)) + " Resolution:" + ('{:>6.4f}'.format(res)) + " Duration:" + ('{:>6.2f}'.format(dur))) # line 3
            t.write("\n")
            t.write('{:>21.13f}{:>26.13f}{:>24.17e}{:>12f}'.format(lowwav,highwav,spacebw,numpoints)) # line 4
            t.write("\n")

            calpy_path = '/Volumes/DAVIS/calpy_em27_neu/spectra_out_demo/140803/*' # the CALPY output files!
            files1 = glob.glob(calpy_path)
            with open(files1[count], 'r') as g:
                for line in g:
                    wave_no, intensity = [float(item) for item in line.split()]
                    if lowwav <= wave_no <= highwav:
                        t.write(str(intensity) + '\n')

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

            subprocess.call(['sfit4Layer0.py', '-bv5', '-fs'],shell=False) #I think this writes the summary file

# this retrieves info from summary and outputs it into data.txt (for readability)
# and graphing_data.txt (for graphing)
            road = '/Volumes/DAVIS/calpy_em27_neu/spectra_out_demo/sfit4_trial' # path to summary file that is produced - not sure where this is usually*        
            for infile in glob.glob(os.path.join(road, 'summary*')):
                lines = open(infile, 'r').readlines()
                #extract info from summary
                x.write('{0} {1} {2} {3} {4}'.format(fitrms, chi2, dofsall, dofstrg, iter))
                x.write('\n')

x.close()
m.close()

0 个答案:

没有答案