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  

Hi i have text file like above. I wish to know how to read only column ( like whole colum below simulated and Measured one by one) if possible i also like to know how to import these column in Excel file using python.

Edited 4 Years Ago by pyTony: Format of the text file and text separated

Use string slicing for ends of data lines, split the numbers and convert to numeric form.

import re
infile = open ("c:/py/over.txt", 'r')
outfile = open('c:/py/output.txt', 'w')
column = 4

for line in infile:
if not re.match('@', line):
line = line.strip()
sline = line.split()
outfile.write(sline[column] + '\n')

I tried in this way but couldnt run the program

Use code blocks for code, here little edited version taking the end part of lines with the numbers and putting them at other file:

infile = open ("over.txt", 'r')
outfile = open('output.txt', 'w')
column = 46
found = False

for line in infile:
    if not '@' in line and not '---' in line  and found:
        found = True


Edited 4 Years Ago by pyTony

Another way to get the same result you can look at.

flag = 1
with open('over.txt') as f, open('output', 'w') as f_out:
    for line in f:
        if line.startswith('  Panicle'):
            flag = 0
        if not flag:
This article has been dead for over six months. Start a new discussion instead.