Opposite of split

John Posner jjposner at optimum.net
Mon Aug 16 20:33:14 CEST 2010


On 8/16/2010 12:44 PM, Alex van der Spek wrote:
>
> Anybody catches any other ways to improve my program (attached), you are
> most welcome.

1. You don't need to separate out special characters (TABs, NEWLINEs, 
etc.) in a string. So:

   bt='-999.25'+'\t''-999.25'+'\t''-999.25'+'\t''-999.25'+'\t'+'-999.25'

... can be ...

   bt='-999.25\t-999.25\t-999.25\t-999.25\t-999.25'

BTW, I think you made a couple of "lucky errors" in this statement. 
Where there are two consecutive apostrophe (') characters, did you mean 
to put a plus sign in between? Your statement is valid because the 
Python interpreter concatenates strings for you:

    >>> x = 'foo''bar'
    >>> x == 'foobar'
    True

    >>> x = 'foo'  'bar'
    >>> x == 'foobar'
    True


2. Take a look at the functions in the os.path module:

   http://docs.python.org/library/os.path.html

These functions might simplify your pathname manipulations. (I didn't 
look closely enough to know for sure.)

3. An alternative to:

   alf.write(tp+'\t'+vf+'\t'+vq+'\t'+al+'\t'+bt+'\t'+vs+'\n')

   ... is ...

   alf.write("\t".join((tp, vf, vq, al, bt, vs)) + "\n")

4. I suggest using a helper function to bring that super-long 
column-heading line (alf.write('Timestamp ...) under control:

   def multi_field_names(base_name, count, sep_string):
       names = [base_name + " " + str(i) for i in range(1, count+1)]
       return sep_string.join(names)

HTH,
John



More information about the Python-list mailing list