On 2011/11/11 02:59 PM, Cranky Frankie wrote:
Thank you for your help on this. Now for "the rest of the story."

I'm trying to build a script to parse IBM AIX DB2 DDL to line up the
data types (it drives me crazy when the column data types are not
lined up). For example, typical create table DDL might be hundreds of
lines long but will look like this:

<SNIP>

Something like this ? I left out checking for valid types etc, just something rudimentary to maybe point you in the right direction.


>>> a = """COLUMN1     DECIMAL(8),
... COLUMN2           CHAR(20),
... COLUMN3                    TIMESTAMP,
... COLUMN4     INTEGER,
... COLUMN5          DATE NOT NULL WITH DEFAULT,
... COLUMN6   CHAR(40)
... """
>>>
>>> for line in a.splitlines():
...     newline = line.split()
...     col_name = newline[0]
...     col_type = newline[1].replace(',', '')
...     field_sep = ',' if ',' in line else ''
...     print '%-40s%s%s' % (col_name, col_type, field_sep)
...
COLUMN1                                 DECIMAL(8),
COLUMN2                                 CHAR(20),
COLUMN3                                 TIMESTAMP,
COLUMN4                                 INTEGER,
COLUMN5                                 DATE,
COLUMN6                                 CHAR(40)

If all you want to space it out nicely then .split(' ', 1) and '%-40s%s' % (part1, part2) should work fine for you.

--

Christian Witts
Python Developer

//
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to