LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-01-2010, 08:46 AM   #1
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
python: how to avoid spaces ?


Hi,

in the code below:
Code:
x= "123.45"
for i in range (1, 10):
  print "%s;" % x,
print
The output is:
Code:
123.45; 123.45; 123.45; 123.45; 123.45; 123.45; 123.45; 123.45; 123.45;
Looks like the "," operator in the print inside the loop is adding a space for its own.
I want to print the sequence WITHOUT spaces. Something like this:
Code:
123.45;123.45;123.45;123.45;123.45;123.45;123.45;123.45;123.45;
but I couldn't figure out how to do this.....

How to print a sequence, inside a loop, without that space ?
 
Old 10-01-2010, 09:07 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
you don't need the for loop

Code:
>>> x="123.45"
>>> ((x +",")*10)[:-1]
'123.45,123.45,123.45,123.45,123.45,123.45,123.45,123.45,123.45,123.45'
or list comprehension
Code:
>>> ','.join([ x for i in range(10) ])
'123.45,123.45,123.45,123.45,123.45,123.45,123.45,123.45,123.45,123.45'
 
Old 10-01-2010, 09:28 AM   #3
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499

Original Poster
Blog Entries: 2

Rep: Reputation: 68
hi ghostdog74 !
the loop in the example, is, well, an example.
in fact I am iterating over rows returned from a sql query and the output is CSV formated.

The problem here is how to use print with the no newline inside the loop to build a row of data. (at the end of the loop I print a newline to just start another loop for the next row.
The extra spaces are unwanted because some colunms are strings (and must be exact strings, without extra spaces) and other columns are float numbers which the extra space cause the data to be interpreted as a text, not as a number, when it is loaded in another system.

Last edited by marozsas; 10-01-2010 at 09:30 AM.
 
Old 10-01-2010, 09:29 AM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
I don't program in python so forgive me if this is a less than divine way of doing this:
Code:
#!/usr/bin/python

import sys

x= "123.45"
for i in range (1, 10):
  sys.stdout.write (x); print ";",
print
I got the hint from here: http://bytes.com/topic/python/answer...t-ending-comma
 
1 members found this post helpful.
Old 10-01-2010, 09:48 AM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ghostdog74 View Post
you don't need the for loop

Code:
>>> x="123.45"
>>> ((x +",")*10)[:-1]
'123.45,123.45,123.45,123.45,123.45,123.45,123.45,123.45,123.45,123.45'
or list comprehension
Code:
>>> ','.join([ x for i in range(10) ])
'123.45,123.45,123.45,123.45,123.45,123.45,123.45,123.45,123.45,123.45'
OMG !!! What do I see in Python ?! There is more than way to do it !
 
Old 10-01-2010, 09:56 AM   #6
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499

Original Poster
Blog Entries: 2

Rep: Reputation: 68
Hi Celine, thanks, your solution works but it is kind of awkward isn't ? At this point, it is more a matter of doing the thing in proper way in first place than solving the problem itself.

In fact, what I already did is:
Code:
myprogram.py | sed -e 's/; /;/g' > output.csv
 
Old 10-01-2010, 10:07 AM   #7
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Hi!

Is my idea awkward? I really don't know - that's the first python code I have ever put together.

But, I question whether your solution in post #6 is 'less awkward' or 'the proper way'; I would think that the 'proper way' would not involve an external tool to remove the space - rather, the space wouldn't be present to begin with. Therefore, I prefer my way over post #6.

I suspect Sergei or Ghostdog or any number of other members have a better grasp that you or I evidently do, so maybe they will tell us what's the "best" way. I figure Ghostdog's second suggestion could be adapted to your original query also.

Cheers!
 
Old 10-01-2010, 10:11 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by marozsas View Post
hi ghostdog74 !
the loop in the example, is, well, an example.
in fact I am iterating over rows returned from a sql query and the output is CSV formated.
show examples, and show desired output!
 
Old 10-01-2010, 10:18 AM   #9
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499

Original Poster
Blog Entries: 2

Rep: Reputation: 68
Quote:
Originally Posted by GrapefruiTgirl View Post
Hi!

But, I question whether your solution in post #6 is 'less awkward' or 'the proper way'; I would think that the 'proper way' would not involve an external tool to remove the space - rather, the space wouldn't be present to begin with. Therefore, I prefer my way over post #6.

Cheers!
Hey, don't get me wrong !

my the code in #6 is not a solution! It is not even a patch ! It is just how I manage to delivery the info other people need and have more time to solve the problem exclusively using python.

So, I still have the problem. I still don't known how to print inside a loop, without newlines and extras spaces.

Of course, like Sergei told, there is more than a way to do this and yours is so good as any other:
Code:
x= "123.45"
output= ""
for i in range (1,10):
  output= output + x + ';'
print output
but the original problem remains: Using the "," operator with print, how to avoid the extra spaces....

Anyway, sorry by the "awkward" - it was not a good word after all....but it is not what I was looking for in first place.

Last edited by marozsas; 10-01-2010 at 12:55 PM.
 
Old 10-01-2010, 10:19 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Sergei Steshenko View Post
OMG !!! What do I see in Python ?! There is more than way to do it !
You are hopelessly misguided. These are not different ways to use a syntax. Not like Perl. So many for loop syntax, so many open() syntax. etc
 
Old 10-01-2010, 10:20 AM   #11
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
No problem

"awkward" may yet turn out to be the right word! I'm interested in seeing what turns up though, if there's an exact solution to what you ask (and I'm sure there is).
 
Old 10-01-2010, 10:22 AM   #12
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by GrapefruiTgirl View Post
x= "123.45"
for i in range (1, 10):
sys.stdout.write (x); print ";",
print
[/code]
you will end up with a ";" at the end.
 
Old 10-01-2010, 10:23 AM   #13
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Quote:
Originally Posted by ghostdog74 View Post
you will end up with a ";" at the end.
Yes, thanks! That part I did figure out.
 
Old 10-01-2010, 10:26 AM   #14
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by marozsas View Post
but the original problem remains: Using the "," operator with print, how to avoid the extra spaces....
Python's print statement is "designed" that way, for simple visualization of your output. The "pythonic" way to do what you want is using str.join() like my second example. And I have been using it as well to print sql statement output so yes, do consider using that method. (there certainly is no need to call extra sed or tools)

that said, if you have Python 2.6+ (or Python 3+)
Code:
>>> from __future__ import print_function # for Python 2.6 
>>> print( *[x for i in range(10)], sep=',')
123.45,123.45,123.45,123.45,123.45,123.45,123.45,123.45,123.45,123.45

Last edited by ghostdog74; 10-01-2010 at 10:47 AM.
 
1 members found this post helpful.
Old 10-01-2010, 12:44 PM   #15
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499

Original Poster
Blog Entries: 2

Rep: Reputation: 68
Quote:
Originally Posted by ghostdog74 View Post
Python's print statement is "designed" that way, for simple visualization of your output.
Ok. I understand that. Makes sense to me. Thanks for the enlightenment.

So I will use others ways not related to "print x," to achieve my goals, since "print x," will, "by design", print an extra space anyway.

I can't use the way you suggested because "x" is not a constant. Just to clarify:

Code:
import kinterbasdb as k

# create a connection to database
con = k.connect(dsn='host:database', user='USER', password='PASSWD')

# Create a Cursor object that operates in the context of Connection con:
cur = con.cursor()

# SQL statement
SQL="select f1, f2, f3, ..fn from table where several conditions;"

# execute the SQL query
cur.execute(SQL)

# interate over the records
out= "row title"
for (f1, f2, f3,...fn) in cur:
   a= functionA (f1, f2, f3, ...fn)
   b= functionB (f1, f2, f3, ...fn)
   x= functionC (a, b)
   out= out + ';' + x
print out
(you see now why I reduced the problem in the first place ? If I am using a database, or how the "x" is evaluated it is not important - just "print x," printing a extra space is the problem.)

Anyway, it was fun, as always.
 
  


Reply

Tags
printing, python, spaces



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM
LXer: Review: Programming in Python 3: A Complete Introduction to the Python Language LXer Syndicated Linux News 0 01-26-2009 04:50 AM
Spaces and escaped spaces pslacerda Linux - Newbie 13 12-20-2008 09:03 AM
how to avoid dependencies conflict to install python-imaging-1.1.6-1.rha forshiv Red Hat 4 09-29-2008 08:49 PM
LXer: Move to python 2.4 / Changing the packaging style for python packages LXer Syndicated Linux News 0 06-13-2006 07:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration