Converting mixed-format .DAT to .CSV (or anything else)

后端 未结 3 389
无人及你
无人及你 2021-01-21 16:14

I have a large collection of DAT files that need to be converted (eventually to a unique file type). The DAT\'s have a mixed amount of whitespace between fields, and the column

3条回答
  •  北恋
    北恋 (楼主)
    2021-01-21 16:55

    Treat those header lines in the input file with all the disdain they deserve. (Or, in other words, read them and discard them.)

    headers='Year Month Day Hour Minute Direct Diffuse2 D_Global D_IR U_Global U_IR Zenith'
    with open ( 'temp.dat') as input_file:
        with open ('temp_2.csv', 'w') as output_file:
            output_file.write('"%s"\n'%'","'.join(headers.split()))
            for count, line in enumerate(input_file):
                if count<4: continue
                outLine = ','.join(line.split())
                output_file.write(outLine + '\n')
    

提交回复
热议问题