读取文件并存储到数据结构

时间:2018-09-10 13:51:14

标签: python dictionary

我必须阅读一个看起来像这样的文件

90004    2.050    2.835    12.260    6487.971
90004 1 -0.127 0.109 -0.070 0.068 -2.382 0.514 0.130 1.404 0.950
90004 2 -0.283 0.223 -0.244 0.221 -1.059 0.893 0.213 0.385 0.996
90004 3 -0.120 0.191 -0.082 0.099 -0.192 0.251 4.158 4.233 0.999
90004 4 -0.084 0.136 -0.041 0.040 -0.257 0.419 4.441 4.551 0.999
90004 5 -0.064 0.343 -0.009 0.012 -0.076 0.241 4.096 4.083 1.000
90004 6 -0.089 0.132 -0.032 0.036 -0.214 0.290 4.828 4.986 0.999

为此,我创建了一个函数

def read_file(pulselist,folder):
    for index1 in range(0, len(pulselist)):
        pulse = pulselist[index1]
        filename=str(pulse)+'.txt'

        with open(folder+filename, 'rt') as f_in:
            # print(f.readline())
            lines = f_in.readlines()
            for index, line in enumerate(lines):
                if index == 0:
                    pass
                elif index ==1:
                    Pulse,MAX_IP,MAX_BVAC,MAX_LID3,MAX_TMAX =lines[index].split()
                else:
                    dummy = lines[index].split()
                    info = dummy

        temp_dict=dict(zip(['A','B','C','D','E','F','G','H','I'], [x for x in info[1:]]))
        temp_dict['pulse'] = int(pulse)
        temp_dict['MAX_IP'] = MAX_IP
        temp_dict['MAX_BVAC'] = MAX_BVAC
        temp_dict['MAX_LID3'] = MAX_LID3
        temp_dict['MAX_TMAX'] = MAX_TMAX
    return temp_dict

这样,我设法只存储了第一行和最后一行。我正在尝试找到一种存储所有行的方法,以便最终字典(或任何其他数据结构)可以正确存储文件中有关行的所有信息。

有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

我以这种方式解决了

def read_polarimetry_file(pulselist,folder):
    pulse_struct = dict()
    for index1 in range(0, len(pulselist)):
        shot = pulselist[index1]
        filename=str(shot)+'.txt'

        with open(folder+filename, 'rt') as f_in:
            # print(f.readline())
            lines = f_in.readlines()
            channels = {}
            channels.setdefault('A', [])
            channels.setdefault('B', [])
            channels.setdefault('C', [])
            channels.setdefault('D', [])
            channels.setdefault('E', [])
            channels.setdefault('F', [])
            channels.setdefault('G', [])
            channels.setdefault('H', [])
            channels.setdefault('I', [])
            for index, line in enumerate(lines):
                if index == 0:
                    pass
                elif index ==1:
                    Pulse,MAX_IP,MAX_BVAC,MAX_LID3,MAX_TMAX =lines[index].split()
                else:
                    dummy = lines[index].split()
                    info = dummy
                    channels['A'].append(float(info[2]))
                    channels['B'].append(float(info[3]))
                    channels['C'].append(float(info[4]))
                    channels['D'].append(float(info[5]))
                    channels['E'].append(float(info[6]))
                    channels['F'].append(float(info[7]))
                    channels['G'].append(float(info[8]))
                    channels['H'].append(float(info[9]))
                    channels['I'].append(float(info[10]))

            pulse=dict()
            pulse['JPN']=shot
            pulse['MAX_IP']=MAX_IP
            pulse['MAX_BVAC']=MAX_BVAC
            pulse['MAX_LID3']=MAX_LID3
            pulse['MAX_TMAX']=MAX_TMAX
            pulse['channels'] = channels


        pulse_struct[str(shot)]=pulse


    return pulse_struct