473,569 Members | 2,438 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stop spaces from appearing when printing to file

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
Jul 18 '05 #1
6 1513
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 ';'
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

Jul 18 '05 #2
> 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

or....

print x + ';' + object + ";" + AN_string + ";" + ascii + ";" + sum
Jul 18 '05 #3
On Sunday 07 November 2004 20:02, 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

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
Jul 18 '05 #4
Kent Johnson wrote:
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 ';'

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


Thanks Kent, this works great.
Jul 18 '05 #5
Kent Johnson wrote:
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 ';'

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)


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

-Scott David Daniels
Scott.DanielsAc m.Org
Jul 18 '05 #6
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 anansispacework s.com )
Anansi Spaceworks http://www.anansispaceworks.com

Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
6726
by: hokiegal99 | last post by:
This script works as I expect, except for the last section. I want the last section to actually remove all spaces from the front and/or end of filenames. For example, a file that was named " test " would be renamed "test" (the 2 spaces before and after the filename removed). Any suggestions on how to do this? import os, re, string print...
135
7405
by: Xah Lee | last post by:
Tabs versus Spaces in Source Code Xah Lee, 2006-05-13 In coding a computer program, there's often the choices of tabs or spaces for code indentation. There is a large amount of confusion about which is better. It has become what's known as “religious war” — a heated fight over trivia. In this essay, i like to explain what is the...
0
1432
by: MLH | last post by:
In an earlier post today, it was mentioned that dbl-spaces caused alignment-justification problems when printing labels. I've never noticed this in reports except for those meant for the production of labels. I routinely embed multiple spaces in strings - sometimes 3, 4 or 5 to achieve a desired effect in the printed output. I do it in...
2
2314
by: CC | last post by:
Hi: I've conjured up the idea of building a hex line editor as a first real Python programming exercise. To begin figuring out how to display a line of data as two-digit hex bytes, I created a hunk of data then printed it: ln = '\x00\x01\xFF 456789abcdef' for i in range(0,15):
0
7618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7679
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6287
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5223
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3657
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
946
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.