在我正在处理的程序中,我正在读取来自.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]
打印日期会给我日期,但是除了[0]之外的任何其他内容都会给出错误
time = columns[1]
IndexError: list index out of range
答案 0 :(得分:1)
使用csv
模块并编写标签分隔值:
import csv
with open('outputfile.csv', 'wb') as outputfile:
writer = csv.writer(outputfile, delimiter='\t')
row = [date, time]
row.extend(map(float, filter2))
for filter in (filter2, filter3, filter4, filter5, filter6, filter7):
row.extend(filter)
row.extend([LZA, SZA, LAM])
writer.writerow(row)
您的输入文件也很可能使用制表符分隔的列,而不是多个空格。