Stop spaces from appearing when printing to file

B

Brad Tilley

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
 
K

Kent Johnson

Brad said:
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 ';'

The spaces are a feature of print. To avoid them, use string formatting
to create a single output string.
print >> x,';',object,";",AN_string,";",ascii,";",sum

Try
print >> x, ';%s;%s;%s;%s' % (object, AN_string, ascii, sum)

Kent
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
 
N

news.west.cox.net

The spaces are a feature of print. To avoid them, use string formatting to
create a single output string.


Try
print >> x, ';%s;%s;%s;%s' % (object, AN_string, ascii, sum)

Kent
or....

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

Dave Reed

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


Instead of using print, open a file and use the "write" method of the
file object. See the documentation for file objects for more detail.

Dave
 
B

Brad Tilley

Kent said:
The spaces are a feature of print. To avoid them, use string formatting
to create a single output string.



Try
print >> x, ';%s;%s;%s;%s' % (object, AN_string, ascii, sum)

Kent

Thanks Kent, this works great.
 
S

Scott David Daniels

Kent said:
The spaces are a feature of print. To avoid them, use string formatting
to create a single output string.



Try
print >> x, ';%s;%s;%s;%s' % (object, AN_string, ascii, sum)

or even:
print >>x, ';'.join([str(v) for v in (object, AN_string, ascii, sum)])

-Scott David Daniels
Scott.DanielsAcm.Org
 
T

Terry Hancock

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top