By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,627 Members | 1,211 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,627 IT Pros & Developers. It's quick & easy.

need help in output table

P: 17
want to read selected lines in a file, then output in one row.
For example:
read the 1st, 3rd ... lines, then output them in the 1st row;
read the 2nd, 4th ... lines, output in the 2nd row;
...

I used several loops. None of them worked.

Thanks.
May 17 '07 #1
Share this Question
Share on Google+
5 Replies


ilikepython
Expert 100+
P: 844
want to read selected lines in a file, then output in one row.
For example:
read the 1st, 3rd ... lines, then output them in the 1st row;
read the 2nd, 4th ... lines, output in the 2nd row;
...

I used several loops. None of them worked.

Thanks.
Use readlines() and then select the lines you want to print out using list comprehension:
Expand|Select|Wrap|Line Numbers
  1. myfile = open("test.txt", "r")
  2. mylines = myfile.readlines()
  3. print [line for line in mylines if mylines.index(line)%2 == 0]
  4. print [line for line in mylines if mylines.index(line)%2 != 0]
  5.  
I think that should work, but I haven't tried it.
May 17 '07 #2

P: 17
Among the solutions, this looks like the simplest and most efficient one.
The only problem is that the output includes some symbols. For example:
the 1st row is
['01\n', '03\n', '05\n']

Since I deal with numbers, I first convert them to int or float, then the output is what I want now:
01 03 05

Although I still don't know how it works if them are strings.
Thanks a lot!!!


Use readlines() and then select the lines you want to print out using list comprehension:
Expand|Select|Wrap|Line Numbers
  1. myfile = open("test.txt", "r")
  2. mylines = myfile.readlines()
  3. print [line for line in mylines if mylines.index(line)%2 == 0]
  4. print [line for line in mylines if mylines.index(line)%2 != 0]
  5.  
I think that should work, but I haven't tried it.
May 19 '07 #3

Expert 100+
P: 511
you just want to gather all odds and evens lines together:
Expand|Select|Wrap|Line Numbers
  1. odd=[];even=[]
  2. for num,line in enumerate(open("file")):
  3.     num+=1
  4.     if num%2==0: even.append(line.strip())
  5.     elif num%2==1: odd.append(line.strip())
  6. print odd
  7. print even
  8.  
May 19 '07 #4

bartonc
Expert 5K+
P: 6,596
Among the solutions, this looks like the simplest and most efficient one.
The only problem is that the output includes some symbols. For example:
the 1st row is
['01\n', '03\n', '05\n']

Since I deal with numbers, I first convert them to int or float, then the output is what I want now:
01 03 05

Although I still don't know how it works if them are strings.
Thanks a lot!!!
One of the first things that you'll need to understand is the difference between binary data and text data (each use different kinds of files). It is very common to save data in text form so that it may be read by people or other programs. Reading data saved as text, you must have you program convert the read info into workable data (in your case, floating point numbers). One cool thing about python is the print statement will take just about anything and convert it back to text for display. I'll give an example in the next post...
May 19 '07 #5

bartonc
Expert 5K+
P: 6,596
Among the solutions, this looks like the simplest and most efficient one.
The only problem is that the output includes some symbols. For example:
the 1st row is
['01\n', '03\n', '05\n']

Since I deal with numbers, I first convert them to int or float, then the output is what I want now:
01 03 05

Although I still don't know how it works if them are strings.
Thanks a lot!!!
I like what ilikepython has done here by reading all the data at once, then working on it. But instead of printing it, we'll convert it, then print the result to see that the conversion worked.
To combine what my friend ilikepython has done with the idea of "parsing" data from a text file:
Expand|Select|Wrap|Line Numbers
  1. nRow = 2   # every other line from the file
  2.  
  3. myfile = open("test.txt", "r")
  4. mylines = myfile.readlines()
  5. myfile.close()
  6.  
  7. nLines = len(mylines)
  8. nColumns = nLines/nRows
  9. table = [[] for i in range(nRows)]   # this is an empty list of empty lists ("array")
  10.  
  11. # Columnate a linear set of data #
  12. for i range(nRows):
  13.     for j in range(0, nColumns, nRows):  # start, stop, step
  14.         table[i][j] = float(mylines[i + j].strip())   #Grab the data and convert stripped text all at once
  15. for row in table:
  16.     for element in row:
  17.         print element,    #  print all the elements of a row on one line using the ','
  18.  
This is not complete because it doesn't handle errors (like what if your data is not rectangular), etc. but should get you going in the right direction.
May 19 '07 #6

Post your reply

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