在我正在处理的程序中,我正在从一个看起来像这样的 .xls 文件中读取数据
2012-SEP-27 04:35:00 0.035173 0.019666 0.001566 -0.002356 0.028054 0.009819 -9999.000000 0.036353 0.021943 0.003582 -0.0006520.025411 0.009548 -9999.000000 0.035703 0.019268 0.003582 -0.0005340.040426 0.010700 -9999.000000 56.904729 95.722267 7.827207
将其分解为列,对数字进行一些筛选(这包括将某些列组合到“过滤器”中),然后尝试将其写回另一个 .xls 文件。我可以将其写入文件,并且输出文件看起来与输入文件相同,但是当我尝试访问单独的列时,我要么不能(它将整个内容视为一列),要么只能访问它字符按性格。
我尝试了多种方式格式化输出,包括:
AA = ('{0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12} {13} {14} {15} {16} {17} {18} {19} {20} {21} {22} {23} {24} {25}'.format(date, time, filter1[0], filter1[1], filter1[2], filter2[0], filter2[1], filter2[2], filter3[0], filter3[1], filter3[2], filter4[0], filter4[1], filter4[2], filter5[0], filter5[1], filter5[2], filter6[0], filter6[1], filter6[2], filter7[0], filter7[1], filter7[2], LZA, SZA, LAM) + '\n')
AA = [date, time, float(filter1[0]), float(filter1[1]), float(filter1[2]), filter2[0], filter2[1], filter2[2], filter3[0], filter3[1], filter3[2], filter4[0], filter4[1], filter4[2], filter5[0], filter5[1], filter5[2], filter6[0], filter6[1],filter6[2], filter7[0], filter7[1],filter7[2],LZA, SZA, LAM]
AA = [(date, time, filter1[0], filter1[1], filter1[2], filter2[0], filter2[1], filter2[2], filter3[0], filter3[1], filter3[2], filter4[0], filter4[1], filter4[2], filter5[0], filter5[1], filter5[2], filter6[0], filter6[1], filter6[2], filter7[0], filter7[1], filter7[2], LZA, SZA, LAM)]
我正在寻找一些关于以列可访问的方式将数据写入文件的一般帮助,我正在处理的代码很难简洁地解释,所以一般示例很好。我是 python 新手,感谢您的帮助和耐心
编辑
writer = csv.writer(outputfile, delimiter = '\t')
row = [date, time]
row.extend(map(float, filter1))
for filter in (filter2, filter3, filter4, filter5, filter6, filter7):
row.extend(filter)
row.extend([LZA, SZA, LAM])
writer.writerow(row)
给了我一个看起来像的输出
2012-SEP-27 04:35:00 -9999.0 -9999.0 -9999.0 -9999.0 -9999.0 -9999.0 -9999.0 -9999.0 -.0 56.904729 95.722267 "7.827207
"
2012-SEP-27 04:39:00 -9999.0 -9999.0 -9999.0 -9999.0 -9999.0 -9999.0 -9999.0 -9999.0 0.0 -9999.0 56.007862 96.708467 "1.784808
"
2012-SEP-27 04:43:00 0.036885 0.038065 0.047741 0.021099 0.0 0.035384 0.034978 0.045806 0.019952 0.013064 0.021955
"
2012-SEP-27 04:47:00 0.038996 0.039075 0.04026 0.020357 0.012688 .036156 0.033027 0.011406 0.011693 0.011979 -9999.0 -9999.0 -9999.0 5
"
2012-SEP-27 04:51:00 -9999.0 -9999.0 -9999.0 0.019505 0.016133 0.018253 146 0.043547 0.012732 0.014486 0.015731 -9999.0 -9999.0 -9999.0 5
"
我正在尝试使用访问每一列
from __future__ import division
import csv
v = open("Pt_2_Test_Data.csv", 'wb')
with open("outputfile.csv", 'rb') as w:
reader = csv.reader(w, delimiter = '\t', quotechar = '|')
for row in w:
columns = row.split(',')
date = columns[0]
time = columns[1]
print date 会给我日期,但是除了 columns[0] 之外的任何东西都会给出这个错误
time = columns[1]
IndexError: list index out of range