Tensorflow 读取Txt和Csv格式数据

def transform_into_csv(file_input_name_a,file_input_name_b,file_output_data,file_output_label):

with open(file_output_data, 'w') as csvfile:
    spamwriter = csv.writer(csvfile, dialect='excel')
    with open(file_input_name_a,'r') as file_txt:
        spamwriter.writerow(['x','y','z','a','b','c'])
        for line in file_txt:
            line_datas = line.strip('\n').split(' ')
            line_in_datas = line_datas[1 : len(line_datas)]
            for i in range(0,int(len(line_in_datas)/6)):
                line_in_six_datas = line_in_datas[i*6:i*6+6]
                spamwriter.writerow(line_in_six_datas)



with open(file_output_label, 'w') as csvfile:
    spamwriter = csv.writer(csvfile, dialect='excel')
    with open(file_input_name_a,'r') as file_txt_a, open(file_input_name_b,'r') as file_txt_b:
        spamwriter.writerow(['direction','distance'])
        for line in file_txt_a:
            line_b = file_txt_b.readline()
            line_datas_a = line.strip('\n').split(' ')
            line_datas_b = line_b.strip('\n').split(' ')
            line_datas_a = line_datas[0: 1]
            line_in_datas_b = line_datas_b[0: 1]
            line_in_datas = line_datas_a + line_in_datas_b
            spamwriter.writerow(line_in_datas)

你可能感兴趣的:(Tensorflow 读取Txt和Csv格式数据)