0
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.

Edited by pyTony: Format of the text file and text separated

3
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by snippsat
0

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

0

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

0

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()

Edited by pyTony

0

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:])
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.