gpt4 book ai didi

python - 将混合格式的 .DAT 转换为 .CSV(或其他格式)

转载 作者:行者123 更新时间:2023-11-28 21:44:44 38 4
gpt4 key购买 nike

我有大量 DAT 文件需要转换(最终转换为唯一的文件类型)。 DAT 的字段之间有混合数量的空白,列标题位于不同的行上。有什么建议吗?

                    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)

我肯定会更改它以解决混合空白,但我不知道如何使用不稳定的列标题。

最终我希望我的标题是:

Year Month Day Hour Minute Direct Diffuse2 D_Global D_IR U_Global U_IR Zenith

最佳答案

以应有的蔑视对待输入文件中的那些标题行。 (或者,换句话说,阅读并丢弃它们。)

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')

关于python - 将混合格式的 .DAT 转换为 .CSV(或其他格式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40269197/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com