EXPERIMENT : KSAS1201 SG CLIMAT CHANGE
DATA PATH : C:\DSSAT45\Sorghum\
TREATMENT 1 : N.American SGCER045
@ VARIABLE SIMULATED MEASURED
-------- ------- --------
Panicle Initiation day (dap) 62 -99
Anthesis day (dap) 115 -99
Physiological maturity day (dap) 160 -99
Yield at harvest maturity (kg [dm]/ha) 8478 -99
Number at maturity (no/m2) 32377 -99
Unit wt at maturity (g [dm]/unit) .0262 -99
嗨我有上面的文本文件。我想知道如何只读取列(如同模拟下的整个列和逐个测量)如果可能的话我也想知道如何使用python在Excel文件中导入这些列。
答案 0 :(得分:1)
将列读入列表的简单方法(假设标题总是6行):
simulated = []
measured = []
with open('input.file') as f:
for l in f.readlines()[6:]:
l = l.split()
simulated.append(l[-2])
measured.append(l[-1])
print simulated
print measured
给出:
['62', '115', '160', '8478', '32377', '.0262']
['-99', '-99', '-99', '-99', '-99', '-99']
请注意,列表仍包含数字的字符串表示形式。使用以下命令解析数字:
simulated.append(float(l[-2]))
measured.append(int(l[-1]))
答案 1 :(得分:0)
Theodros为阅读文件提供了一个很好的答案。要将其保存为excel,您可以将其保存为csv(使用csv
模块),也可以尝试xlwt
模块(可从PyPi获得)。
答案 2 :(得分:0)
from itertools import islice
with open('some.file') as fin:
for line in islice(fin, 6, None):
desc, simulated, measured = ' '.join(line.split()).rsplit(' ', 2)
# do any necessary conversions
然后查看xlwt
/ csv
或XML或其他内容,以便将其读回其他内容...