Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python正则表达式错误:NameError:name';re';没有定义_Python_Regex - Fatal编程技术网

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

python正则表达式错误:NameError:name';re';没有定义,python,regex,Python,Regex,我有一些代码可以读取信息文件,并使用python的正则表达式提取信息,然后将其写入新文件。当我在自己的脚本中单独测试这部分代码时,它工作得非常好。但是,当我将其添加到代码的其余部分时,会出现以下错误: NameError: name 're' is not defined 下面是我的全部代码。正则表达式部分很明显(所有re.search命令): 导入全局 导入子流程 导入操作系统 导入日期时间 将matplotlib.pyplot作为plt导入 导入csv 进口稀土 导入ntpath x=打开

我有一些代码可以读取信息文件,并使用python的正则表达式提取信息,然后将其写入新文件。当我在自己的脚本中单独测试这部分代码时,它工作得非常好。但是,当我将其添加到代码的其余部分时,会出现以下错误:

NameError: name 're' is not defined
下面是我的全部代码。正则表达式部分很明显(所有
re.search
命令):

导入全局
导入子流程
导入操作系统
导入日期时间
将matplotlib.pyplot作为plt导入
导入csv
进口稀土
导入ntpath
x=打开('data.txt','w')
m=打开('graphing_data.txt','w')
ckopuspath='/Volumes/DAVIS/sfit ckopus/ckopus'
命令_init='sfit4Layer0.py-bv5-fh'
subprocess.call(command_init.split(),shell=False)
打开('/Volumes/DAVIS/calpy_em27_neu/spectra_out_demo/info.txt',rt')作为信息文件:#由calpy创建的info.txt文件
对于计数,枚举(信息文件)中的行:
打开('\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
lat=re.search('位置纬度:\s*([^;]+)',行,re.IGNORECASE)。组(0)
横向=横向拆分()
纬度=纬度[3]
横向=浮动(横向)
lon=re.search('位置经度:\s*([^;]+)',行,re.IGNORECASE)。组(0)
lon=lon.split()
lon=lon[3]
lon=浮动(lon)
日期=重新搜索('测量时间\(UTC\):([^;]+)',行)。组(0)
date=date.split()
yeardate=日期[4]
yeardate=yeardate.split('-'))
year=int(yeardate[0])
月=整数(年日期[1])
日期=整数(年日期[2])
时间=日期[5]
time=time.split(“:”)
小时=整数(时间[0])
分钟=整数(时间[1])
秒=浮动(时间[2])
dur=re.search('测量持续时间\[s\]:([^;]+),第行)。组(0)
dur=dur.split()
dur=浮动(dur[4])
numpoints=re.search('一次扫描的值数:\s*([^;]+)',行,re.IGNORECASE).组(0)
numpoints=numpoints.split()
numpoints=float(numpoints[6])
fov=re.search('semi-fov\[rad\]:\s*([^;]+)',行,re.IGNORECASE)。组(0)
fov=fov.split()
视野=视野[3]
fov=浮动(fov[1:])
sza=re.search('太阳方位角\[deg\]:\s*([^;]+)',直线,re.IGNORECASE)。组(0)
sza=sza.split()
sza=浮动(sza[3])
信噪比=0.0000
净资产收益率=6396.2
res=0.5000
lowwav=re.search('第一个波数:\s*([^;]+)',第行,re.IGNORECASE)。组(0)
lowwav=lowwav.split()
lowwav=浮动(lowwav[2])
highwav=re.search('最后一个波数:\s*([^;]+)',行,re.IGNORECASE)。组(0)
highwav=highwav.split()
highwav=浮动(highwav[2])
spacebw=(高波-低波)/numpoints
d=datetime.datetime(年、月、日、小时、分钟、秒)
t、 写入({:>12.5f}{:>12.5f}{:>12.5f}{:>12.5f}{:>8.1f})。格式(sza、roe、lat、lon、snr))#第1行
t、 写入(“\n”)
t、 写入({:>10d}{:>5d}{:>5d}{:>5d}{:>5d})。格式(年、月、日、时、分、秒)第2行
t、 写入(“\n”)
t、 写入({:%Y/%m/%d%H:%m:%S}.格式(d))+“UT太阳方位角:”+({:>6.3f}.格式(sza))+“分辨率:”+({:>6.4f}.格式(res))+“持续时间:”+({:>6.2f}.格式(dur))#第3行
t、 写入(“\n”)
t、 写入({:>21.13f}{:>26.13f}{:>24.17e}{:>12f})。格式(lowwav、highwav、spacebw、numpoints))#第4行
t、 写入(“\n”)
calpy_path='/Volumes/DAVIS/calpy_em27_neu/spectra_out_demo/140803/*'#calpy输出文件!
files1=glob.glob(计算路径)
将open(files1[count],'r')作为g:
对于g行:
波号,强度=[行中项目的浮动(项目)。拆分()

如果是lowwav,请包含对错误的完整回溯。您发布的代码顶部显然有一行
import-re
,因此不能成为此错误的原因。当您将其与其他代码集成时,是否忘记了
import-re
。@MartijnPieters抱歉,什么是完全回溯(编程非常新)?@alli:当您在控制台或终端中运行脚本时,
name错误:未定义名称“re”
是回溯的最后一行;在此之前还有更多的文本,以
Traceback
.Traceback开始(最近一次调用最后一次):文件“total.py”,第57行,在lat=re.search('位置纬度:\s*([^;]+)',行,re.IGNORECASE)。组(0)名称错误:名称“re”未定义请务必为您的错误提供完整的回溯。您发布的代码顶部显然有一行
import-re
,因此不能成为此错误的原因。当您将其与其他代码集成时,是否忘记了
import-re
。@MartijnPieters抱歉,什么是完全回溯(编程非常新)?@alli:当您在控制台或终端中运行脚本时,
name错误:未定义名称“re”
是回溯的最后一行;在此之前还有更多文本,从
Traceback
.Traceback开始(最近一次调用最后一次):文件“total.py”,第57行,在lat=re.search('位置纬度:\s*([^;]+)',行,re.IGNORECASE)。组(0)名称错误:未定义名称“re”
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()