需要帮助以python读取文件

rahul_2008aust

我正在尝试读取文件,并且文件包含各种数据。文件类型的示例在下面给出。

[CIRCUIT1]
CIRCUITNAME=CIRCUIT1
00.12 12/20 2.3 23.6
00.12 12/20 2.3 23.6
00.42 12/20 2.2 23.3
00.42 12/20 2.2 23.3

[CIRCUIT2]
CIRCUITNAME=CIRCUIT2
00.12 12/20 2.2 26.7
00.12 12/20 2.2 26.7
00.42 12/20 2.2 26.5
00.42 12/20 2.2 26.5
00.42 12/20 2.2 26.5

[AMBIENT]
00.42 12/20 8.6
01.42 12/20 8.6
02.42 12/20 8.6
03.42 12/20 8.7
04.42 12/20 8.8
05.42 12/20 8.6
06.42 12/20 8.7

现在,我定义了一个仅返回circuit1的第3列和第4列的函数。但是应该返回日期和时间格式,以后再定义。但是我得到索引超出范围错误。

def load_ci(filepath):
  fileObj=open(filepath, 'r')
  time_1=[],time_2=[],t=0,ti=0,loadCurrent_1=[],surfaceTemp_1=[],loadCurrent_2=[],surfaceTemp_2=[],ambient=[]
  read=0
  for line in fileObj:
    if not line.strip():
        continue    
    if read==1:
        if '[AMBIENT]' in line:
            read=3
            continue
        elif  'CIRCUITNAME=CIRCUIT2' in line: read=2
        else:
            if line!='\n' and '[CIRCUIT2]' not in line:
                point=line.split(' ')
                t=(float(point[0]))
                ti=int(t)*3600+(t-int(t))*60*100
                time_1.append(ti)
                loadCurrent_1.append(float(point[2]))
                surfaceTemp_1.append(float(point[3]))
    if read==2:
        if '[AMBIENT]' in line:
            read=3
            continue
        elif  'CIRCUITNAME=CIRCUIT2' in line: read=2
        else:
            if line!='\n' and '[CIRCUIT2]' not in line:
                point=line.split(' ')
                t=(float(point[0]))
                ti=int(t)*3600+(t-int(t))*60*100
                time_2.append(ti)
                loadCurrent_2.append(float(point[2]))
                surfaceTemp_2.append(float(point[3]))
    if read==3:
        if line!='\n':
            point=line.split(' ')
            ambient.append(float(point[2]))
    if 'CIRCUITNAME=CIRCUIT1' in line: read=1
return  np.array(loadCurrent_1),np.array(surfaceTemp_1),np.array(loadCurrent_2),np.array(surfaceTemp_2),np.array(ambient),np.array(time_1),np.array(time_2)
mtadd

在检测到包含[AMBIENT]的行之后,需要前进到下一行,同时将读取状态更改为3。read = 3之后的代码中的两个点上,在要检查[AMBIENT]的位置添加一个继续语句

此外,请从以下位置更改对[CIRCUIT2]的代码检查

if line != '\n' and line != '[CIRCUIT2]':

if line != '\n' and '[CIRCUIT2]' not in line:

如果要忽略空行,可以在循环开始时添加一个检查,例如:

if not line.strip():
   continue

我对问题中的代码进行了重新设计,以从环境数据中分解解析电路,以简化状态管理。我绕过文件对象,利用它的迭代状态来跟踪我们在给定点处文件中的位置。文件的各个部分以“ [...]”开头,并以空行结尾,因此我可以利用这一点。为了方便起见,我将所有电路数据归为一个字典,但是如果您愿意的话,也可以将其归类为完整的类。

import numpy as np


def parseCircuit(it, header):
    loadCurrent, surfaceTemp, time = [], [], []
    for line in it:
        line = line.strip()
        if not line:
            break
        elif line.startswith('CIRCUITNAME='):
            name = line[12:]
        else:
            point=line.split(' ')
            h, m = map(int, point[0].split('.'))
            time.append(h * 3600 + m * 60)
            loadCurrent.append(float(point[2]))
            surfaceTemp.append(float(point[3]))
    return {'name': name,
            'surfaceTemp': np.array(surfaceTemp),
            'loadCurrent': np.array(loadCurrent),
            'time': np.array(time)}


def parseAmbient(it, header):
    ambient = []
    for line in it:
        line = line.strip()
        if not line:
            break
        point=line.split(' ')
        ambient.append(float(point[2]))
    return np.array(ambient)


def load_ci(filepath):
    fileObj=open(filepath, 'r')
    circuits = {}
    ambient = None
    for line in fileObj:
        line = line.strip()                  # remove \n from end of line
        if not line:                         # skip empty lines
            continue
        if line.startswith('[CIRCUIT'):
            circuit = parseCircuit(fileObj, line)
            circuits[circuit['name']] = circuit
        elif line.startswith('[AMBIENT'):
            ambient = parseAmbient(fileObj, line)
    return circuits, ambient


print load_ci('test.ci')

输出

({'CIRCUIT2': {'loadCurrent': array([ 2.2,  2.2,  2.2,  2.2,  2.2]), 'surfaceTemp': array([ 26.7,  26.7,  26.5,  26.5,  26.5]), 'name': 'CIRCUIT2', 'time': array([ 720,  720, 2520, 2520, 2520])}, 'CIRCUIT1': {'loadCurrent': array([ 2.3,  2.3,  2.2,  2.2]), 'surfaceTemp': array([ 23.6,  23.6,  23.3,  23.3]), 'name': 'CIRCUIT1', 'time': array([ 720,  720, 2520, 2520])}}, array([ 8.6,  8.6,  8.6,  8.7,  8.8,  8.6,  8.7]))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章