将混合格式的.DAT转换为.CSV(或其他任何格式) [英] Converting mixed-format .DAT to .CSV (or anything else)
本文介绍了将混合格式的.DAT转换为.CSV(或其他任何格式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有大量的DAT文件需要转换(最终转换为唯一的文件类型). DAT在字段之间具有混合的空白,并且列标题位于不同的行上.有什么建议吗?
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 headers are on different lines. Any advice?
ALT_RAD
ALT_RAD2
DIRECT D_GLOBAL U_GLOBAL Zenith
Year Mn Dy Hr Mi DIFFUSE2 D_IR U_IR
2004 9 1 0 1 1.04 79.40 78.67 303.58 61.06 310.95 85.142
2004 9 1 0 2 0.71 74.36 73.91 303.80 57.82 310.92 85.171
2004 9 1 0 3 0.67 71.80 71.64 304.25 56.84 310.98 85.199
2004 9 1 0 4 0.75 74.35 74.83 304.21 59.68 310.89 85.227
我有一个基本的脚本:
import sys
with open(sys.argv[1], r) as input_file:
newLines = []
for line in input_file:
newLines.append(newLine)
我肯定会更改以解决混合空格,但是我不知道如何使用不正确的列标题.
Which I will certainly change to account for mixed whitespace, but I don't know how to work with the wonky column headers.
最终我希望标题是:
Year Month Day Hour Minute Direct Diffuse2 D_Global D_IR U_Global U_IR Zenith
推荐答案
使用所有应有的轻蔑对待输入文件中的那些标题行. (或者换句话说,阅读并丢弃它们.)
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')
这篇关于将混合格式的.DAT转换为.CSV(或其他任何格式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文