473,387 Members | 3,801 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,387 software developers and data experts.

need help in output table

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
5 2132
ilikepython
844 Expert 512MB
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
runsun
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
ghostdog74
511 Expert 256MB
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
6,596 Expert 4TB
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
6,596 Expert 4TB
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

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

Similar topics

5
by: wardellcastles | last post by:
I need to transform from an xml specification to a document with a "book-style" table of contents; one with page numbers, not html hyperlinks (The output of the XSLT is NOT html). For example,...
5
by: Glenn | last post by:
Hi! Server info - Win2K3 Server +SP1 with 1 GB Memory and 1.5 GB Virtual Memory SQL Server 2000 Enterprise Edition + SP3 running on this. Required result - Create a SQL Script that will...
25
by: Bjørn T Johansen | last post by:
I need to write a SQL that calculates the interval between a start time and a stop time. This is the easy part. The problem is that I only have the time part, i.e. no date, so how can I be sure to...
7
by: K. Crothers | last post by:
I administer a mechanical engineering database. I need to build a query which uses the results from a subquery as its input or criterion. I am attempting to find all of the component parts of...
0
by: ward | last post by:
Greetings. Ok, I admit it, I bit off a bit more than I can chew. I need to complete this "Generate Report" page for my employer and I'm a little over my head. I could use some additional...
4
by: Hemant Shah | last post by:
Folks, I am having problem with an application that uses static SQL, the application basically browses through the table given start and end key most of the time it is processed from begining to...
6
by: Hemant Shah | last post by:
Folks, I am having trouble with a query. DB2 does not use index, it does relation scan of the table. I am running DB2 UDB 8.2 on Fedora Core release 4 (Stentz) # db2level DB21085I ...
25
by: Jon Slaughter | last post by:
I have some code that loads up some php/html files and does a few things to them and ultimately returns an html file with some php code in it. I then pass that file onto the user by using echo. Of...
16
by: Okonita via DBMonster.com | last post by:
Hi all, I am comming along with all this Linus/DB2/scripting business...I am no longer scared of it!! (LOL). But, I need to create a .ksh script that does a REORGCHK and output only tables...
0
by: LanaR | last post by:
Hello, one sql statement is causing severe performance issue. The problem occurs only in UDB environment, the same statemnt on the mainframe is running fine. I have an explain output from the sql....
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.