Stop spaces from appearing when printing to file

Terry Hancock hancock at anansispaceworks.com
Mon Nov 8 03:12:36 CET 2004


On Sunday 07 November 2004 07:02 pm, Brad Tilley wrote:
> I'm printing some info into a txt file that will be uploaded into a 
> MySQL DB. I use ';' as field separaters. How can I stop spaces from 
> appearing on both sides of the ';'
> 
> When I do this:
> 
> print >> x,';',object,";",AN_string,";",ascii,";",sum
> 
> My file looks like this:
> 
> ;12345 ; 23456 ; [1,2,3,4,5] ; 15
> 
> I need it to look like this to make the DB happy:
> 
> ;12345;23456;[1,2,3,4,5];15

Generally speaking, you'll be better off to produce the string
you want and then print it, rather than relying on the print
command's syntax.  You'll find it much more intuitive to do
either:

print x+';'+object+";"+AN_string+";"+ascii+";"+sum

or

print "%s;%s;%s;%s;%s" % (x, object, AN_string, ascii, sum)

In other words, use string operators, then print the result.

Cheers,
Terry

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks  http://www.anansispaceworks.com




More information about the Python-list mailing list