在Python 3中检查文件列表中的字符串

时间:2019-02-21 15:00:27

标签: python python-3.x

我正在尝试让我的代码遍历文件列表并从每个文件中检索一条信息。列表中有618个.txt文件,其中606个包含我需要的信息。我需要我的代码来检查每个文件是否包含字符串“ J幅度”,如果包含,则获取相关值。如果字符串不存在,我希望改为添加数字-99.9,这样我的列表长度仍为618个项目。

这是我到目前为止编写的代码:

def find_Jmag (files):
    mags = []
    for each in files:
        with open(each) as f:
            if "J magnitude" in f:
                for line in f:
                    if "J magnitude" in line:
                        mag = float((line.split()[4]))
                        mags.append(mag)
            else:
                mag = -99.9
                mags.append(mag)
    return mags
Jmags = np.array(find_Jmag(txtfiles))

我现在得到的输出是:

  

[-99.9 -99.9 -99.9 ... -99.9 -99.9 -99.9]

这意味着由于某种原因,每个文件都无法满足其中包含“ J大小”的条件,这是不对的。

这是每个文件的样例:

#  ----------------------------------------------------------------------------------

# SpeX prism spectrum of 2MASP J0345432+254023 (J03454316+2540233)

# Originally observed on 2003 Sep 05

# Average resolution = 75

# Originally published in Burgasser & McElwain (2006) AJ, 131, 1007

#

# PLEASE CITE THE ORIGINAL DATA REFERENCE WHEN PUBLISHING OR PRESENTING THESE DATA

#

# Optical spectral type: L0

# Near infrared spectral type: L1+/-1

# J magnitude = 13.997

# H magnitude = 13.211

# Ks magnitude = 12.672

#

#  Wavelength (micron)   F_lambda (normalized)  Noise (normalized as F_lambda)

#  ----------------------------------------------------------------------------------

0.657669    0.155371    0.0956746

0.659854    0.0718279   0.0411391

0.662031    -0.0147441  0.0684986

0.664202    -0.0543488  0.0497614

我不确定我哪里出错了,我们将不胜感激!

3 个答案:

答案 0 :(得分:1)

好像J magnitude" in f:检查失败。与其进行检查,不如尝试在if "J magnitude" in line:内放入一个True标记,如果发现“ J幅度”,然后如果标记为mag = -99.9则执行False

def find_Jmag (files):
    mags = []
    for each in files:
        with open(each) as f:
            is_found = False
            for line in f:
                if "J magnitude" in line:
                    is_found = True
                    mag = float((line.split()[4]))
                    mags.append(mag)
            if not is_found:
                mag = -99.9
                mags.append(mag)
    return mags
Jmags = np.array(find_Jmag(txtfiles))

答案 1 :(得分:1)

import re
def find_Jmag(files):
    mags =[]
    re = re.compile(r'J magnitude =(.*)\n')
    for file in files:
        data = re.findall(open(file).read())
        if len(data) != 0 :
            mags.append(int(data[0].strip()))
        else:
            mags.append(-99.9)
    return mags

答案 2 :(得分:0)

这不起作用,因为您正在通过说if "J magnitude" in f:在文件名中查找字符串“ J大小”,因此它将在每种情况下返回您的else值。这可以很容易地修改。我将文件示例复制到2个文件中,分别为file1.txt和file2.txt。在file2.txt中,我取出了“ J幅度”行,因此它应该返回false。我在下面提供的解决此问题的关键是:

if "J magnitude" in open(each).read():

这将搜索每个文件的内容,而不是文件名。

代码如下:

file_list = ['file1.txt', 'file2.txt']

mags = []
for each in file_list:
    if "J magnitude" in open(each).read(): # this searches contents of file instead of filename
        with open(each) as f:
            for line in f:
                if "J magnitude" in line:
                    mag = float((line.split()[4]))
                    mags.append(mag)
    else:
        mag = -99.9
        mags.append(mag)

print(mags)

哪些印刷品:

[13.997, -99.9]