python nameerror错误_python - python regex错误:NameError:未定义名称“ re” - 堆栈内存溢出...

我有一些代码可以读取信息文件,并使用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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在安装 FreeCAD 时遇到依赖问题,提示缺少 libboost-regex1.74.0-icu6,解决这个问题需要安装所需的依赖项。 libboost-regex1.74.0-icu6 是 Boost 库的一个子模块,用于提供正则表达式的功能。缺少这个库可能是由于系统中缺少对应版本的 Boost 库所导致的。 解决方法如下: 1. 首先,需要确认操作系统中是否已经安装了 Boost 库。可以使用以下命令进行确认: ``` dpkg -l | grep libboost ``` 如果没有安装 Boost 库,请先安装对应版本的 Boost 库。可以使用以下命令进行安装: ``` sudo apt-get install libboost-all-dev ``` 2. 在安装 libboost-regex1.74.0-icu6 之前,需要添加一个 Ubuntu PPA(个人软件包存档)仓库,该仓库提供了最新的 FreeCAD 软件包及其依赖项。可以使用以下命令添加该仓库: ``` sudo add-apt-repository ppa:freecad-maintainers/freecad-stable ``` 3. 添加完仓库后,需要更新系统的软件包信息。可以使用以下命令进行更新: ``` sudo apt-get update ``` 4. 更新完软件包信息后,可以尝试安装 FreeCAD 以及所需的依赖项。可以使用以下命令进行安装: ``` sudo apt-get install freecad ``` 5. 安装完成后,可以使用以下命令启动 FreeCAD: ``` freecad ``` 以上是通过添加 PPA 仓库并安装对应的依赖项来解决 FreeCAD 安装中缺少 libboost-regex1.74.0-icu6 的问题。如果问题仍然存在,建议参考官方文档或社区支持寻求更多帮助。 ### 回答2: 在安装FreeCAD时遇到依赖项错误 "libfreecad-python3-0.19 : 依赖: libboost-regex1.74.0-icu6" 可能意味着你的系统缺少 libboost-regex1.74.0-icu6 这个库文件。 解决这个问题的一种方法是手动安装缺少的依赖项。你可以按照以下步骤进行操作: 1. 打开终端并输入以下命令以更新系统软件包列表: ``` sudo apt update ``` 2. 安装 libboost-regex1.74.0-icu6 依赖项: ``` sudo apt install libboost-regex1.74.0-icu6 ``` 3. 继续安装 FreeCAD: ``` sudo apt install freecad ``` 这样,系统应该能够正确安装 FreeCAD 软件包,并满足所有依赖项。 如果以上步骤仍然无法解决问题,你可以尝试从官方网站下载 FreeCAD 的最新版本,并根据其提供的安装说明进行安装。有时官方网站的版本可能与软件源中的版本不同,可能会解决某些依赖性问题。 总之,安装 FreeCAD 时遇到依赖项错误通常可以通过安装缺少的依赖项来解决。希望以上信息能够对你有所帮助。 ### 回答3: 要解决在安装FreeCAD时出现的libfreecad-python3-0.19报错,依赖于libboost-regex1.74.0-icu6的问题,您可以按照以下步骤解决。 1. 首先,检查您的系统上是否已安装libboost-regex1.74.0-icu6。您可以使用以下命令进行检查: ``` dpkg -l libboost-regex1.74.0-icu6 ``` 2. 如果返回结果显示未安装libboost-regex1.74.0-icu6,您可以尝试使用以下命令安装它: ``` sudo apt-get install libboost-regex1.74.0-icu6 ``` 3. 如果上述命令无法安装libboost-regex1.74.0-icu6,可能是因为您的软件包索引过期。您可以通过运行以下命令更新软件包索引: ``` sudo apt-get update ``` 4. 更新软件包索引后,再次尝试安装libboost-regex1.74.0-icu6: ``` sudo apt-get install libboost-regex1.74.0-icu6 ``` 5. 完成安装后,您可以再次尝试安装FreeCAD,应该不会再出现libfreecad-python3-0.19的依赖报错。 如果在安装过程中遇到其他问题,建议查看相关错误消息,以便进一步确定出错的原因,并尝试在开放源代码社区中寻求帮助或提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值