[Python-talk] Module to massage text file for better output?

Bill Sconce sconce at in-spec-inc.com
Sun Jul 15 12:52:25 EDT 2007


On Sat, 14 Jul 2007 09:51:46 -0400
Hewitt_Tech <hewitt_tech at comcast.net> wrote:

> Before I go to the trouble of writing a customer report processor

Suggestion 1: don't be put off by writing a few lines of code.
Not if they're going to be in Python.

One should *not* spend 4 hours writing something which someone later
shows you exists in the standard library.  But neither should one spend
4 hours looking for existing code which you could write yourself in
30 minutes...


> I was 
> wondering if Python has a module that might expedite writing the 
> program. Specifically I have a file generated by a SQL utility that 
> creates a large text file with lot's of data aligned in rows/columns. 
> The problem is that some of the data is aligned in fixed size fields 
> that are much larger than necessary. What I'd like to have for output is 
>    data in fixed size fields that are more optimal. So for example I 
> might have a user's name field that is 80 characters wide but the 
> largest data never exceeds 35 characters. I want the extra characters 
> gone but still have good alignment to make the report more readable.

Suggestion 2:
    col =           field.strip()       # Get rid of whitespace
    col =           field.strip()[:33]  # Truncate if "too" long
    col = '%-35s' % field.strip()[:33]  # Make column fixed width again 


[...]
> I suspect by the time I fiddle with the output I'd 
> probably end up spending 4 or more hours getting something that would
> do the job.

As well as getting some expertise to do it in 30 minutes next time...


> P.S. If I wrote this program I could probably read the report to EOF 
> recording the maximum size of each datum for a particular field and
> then rewrite the data making it fit the fields better.

Exactly!  Because it's Python you'll be able to one-plus it later in
a way which other languages only promise..


readability_counts'ly yrs,

Bill


More information about the Python-talk mailing list