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  

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.

Recommended Answers

All 6 Replies

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

Can you please describe in detail.....

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')
infile.close()
outfile.close()

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:
        outfile.write(line[column:])
    else:
        found = True

infile.close()
outfile.close()

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:
            f_out.write(line[-19:])
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.