Formatting input text file

Edwin.Madari at VerizonWireless.com Edwin.Madari at VerizonWireless.com
Thu Aug 14 15:59:22 CEST 2008


save following code in script.py, and run it as 'python script.py <your-data-file>'
with your sample data this prints out following which is what you are looking for (i think)
3.08333 9.05526 3.13581
4.08322 4.02526 3.95891
========================================================
import sys

data = []
row = []
fh = open(sys.argv[1])
for line in fh:
   line = line.strip()
   if not line or line.startswith('....'):
      continue
   if line.startswith('X'):
      if row:
         data.append(row)
         row = []
      continue
   row.append(line.split()[1])
if row:
   data.append(row)

for  i in data:
   print ' '.join(i)
=====================================

good luck.
Edwin
-----Original Message-----
From: python-list-bounces+edwin.madari=verizonwireless.com at python.org
[mailto:python-list-bounces+edwin.madari=verizonwireless.com at python.org]
On Behalf Of victor.herasme at gmail.com
Sent: Thursday, August 14, 2008 7:07 AM
To: python-list at python.org
Subject: Formatting input text file


Hi,

it's me again with tons of questions. I hava an input file structured
like this:

                  X               XYData-1

                1.                 3.08333
                2.                 9.05526
                3.                 3.13581
                .......

                  X               XYData-2

                1.                 4.08322
                2.                 4.02526
                3.                 3.95891
                ...............

i want to format it so i only get the second column, in order to place
it in a mxn matrix. Let's say i want this:

number1   number2   number3
number4   number5   number6

i hope it is not too hard to do. Any help greatly apreciated. Thanks,

Victor
--
http://mail.python.org/mailman/listinfo/python-list


The information contained in this message and any attachment may be
proprietary, confidential, and privileged or subject to the work
product doctrine and thus protected from disclosure.  If the reader
of this message is not the intended recipient, or an employee or
agent responsible for delivering this message to the intended
recipient, you are hereby notified that any dissemination,
distribution or copying of this communication is strictly prohibited.
If you have received this communication in error, please notify me
immediately by replying to this message and deleting it and all
copies and backups thereof.  Thank you.





More information about the Python-list mailing list