473,320 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

reading a column from text

Hi I have large text file as follows.

EXPERIMENT : KSAS1201 SG CLIMAT CHANGE STUDY ON KANSAS
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) 0.0262 -99

I wish to read only a certain column from this file such as just a column below simulation and measured
Jun 6 '12 #1
1 1583
Glenton
391 Expert 256MB
Hi

We often like to see the code you've attempted so far, what results you've got and what you're looking for.

However, the simple way to do this is to use regular expressions.

For example:
Expand|Select|Wrap|Line Numbers
  1. import re
  2. myFile=open("input.txt")
  3.  
  4. result={}   # dictionary for results  
  5. p=re.compile(r'(.*) (.*) (.*)')
  6. start=False
  7. for line in myFile:
  8.     if not start: #continue until the data starts
  9.         if line[0]=="@":
  10.             start=True
  11.         continue
  12.     m=p.match(line)
  13.     result[m.group(1)] = [float(m.group(2)),float(m.group(3))]
  14.  
  15. for k in result:
  16.     print "##############"
  17.     print "key:",k
  18.     print "value:",result[k]
uses the fact that regular expressions are by default hungry, and therefore finds the last two spaces in the lines. This yields the following:
Expand|Select|Wrap|Line Numbers
  1. >>> 
  2. ##############
  3. key: Unit wt at maturity (g [dm]/unit)
  4. value: [0.0262, -99.0]
  5. ##############
  6. key: Physiological maturity day (dap)
  7. value: [160.0, -99.0]
  8. ##############
  9. key: Yield at harvest maturity (kg [dm]/ha)
  10. value: [8478.0, -99.0]
  11. ##############
  12. key: Anthesis day (dap)
  13. value: [115.0, -99.0]
  14. ##############
  15. key: Number at maturity (no/m2)
  16. value: [32377.0, -99.0]
  17. ##############
  18. key: Panicle Initiation day (dap)
  19. value: [62.0, -99.0]
  20. >>> 
Jun 7 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
1
by: rodchar | last post by:
hey all, is there a quick way to read.all the contents of a text file, search for the text, and if it finds that text to read the entire line? thanks, rodchar
1
by: mart2006 | last post by:
Hi, I'm currently reading a text file via PHP which, in itself, is very easy. However I want to specifically get one word from the text file and assign it as a variable and I'm struggling like...
5
by: Z.K. | last post by:
In C#, using the StreamReader, how do I detect when you get to the end of line. I am reading a text file using the Read() function and I need to detect the \n\r, but everything I try does not...
4
by: Vikas Kumar | last post by:
propertyDescription += "<br>" + lblpropertyDescription.Text; //here i am reading some text from text area i test wrting "p" in my text area it wrks fine but when i write <pin my text...
1
by: SivaramaGuru | last post by:
Rather than table format, is there any other way to display two column text using HTML?
1
by: engggirl3000 | last post by:
Another question I have, what is the difference between reading a text file to a program and opening a text file in the program? A sample of one of the text files is formatted like this: 3 ...
2
by: thanawala27 | last post by:
Hi, I'm facign a strange problem in reading a text file. The contents of my text file is: A1;B1;C1;D1 A2;B2;C2;D2 A3;B3;C3;D3
2
by: friend.blah | last post by:
i have a text file lets say in this format abc abs ajfhg agjfh fhs ghg jhgjs fjhg dj djk djghd dkfdf .... .... ...... i want to read the first line at certain time for eg : at 10clk
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.